springmvc MultipartFile转file和图片压缩

/**
* 文件上传
* @param image
* @param request
* @param session
* @return
*/
@RequestMapping(method = RequestMethod.POST,value="/uploadfile")
@ResponseBody
public Map<String,Object> uploadFile(@RequestParam MultipartFile uploadFileName,HttpSession session){
Map<String,Object> resultmap = new HashMap<String,Object>();
String realPath = session.getServletContext().getRealPath(Constant.IMGURL);
String headname = uploadFileName.getOriginalFilename();


String path = realPath+"/"+headname;
File file = new File(path);
if(file.exists()){
resultmap.put("status_code","1");
return resultmap;
}
try {
FileUtil.upFile(uploadFileName.getInputStream(), headname, realPath);
} catch (IOException e) {
e.printStackTrace();

}

// MultipartFile转file

// CommonsMultipartFile cf= (CommonsMultipartFile)uploadFileName; 
// DiskFileItem fi = (DiskFileItem)cf.getFileItem(); 
// File f = fi.getStoreLocation();

        String string = FileType.getFileType(file);
        int type = FileType.getType(string);
        
        String fileName = file.getName();
if (type == FileType.IMAGE) {
try {
FileTransform.createThumbPic(file,realPath);
} catch (IOException e) {
e.printStackTrace();
}
} else if (type == FileType.AUDIO) {
if (fileName.endsWith(".amr")) {
FileTransform.convertAmrToMp3(fileName,realPath);
}
}
resultmap.put("status_code","0");
return resultmap;
}


package com.enway.util;


import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;


import javax.imageio.ImageIO;




public class FileTransform {
/**
* 使用ffmpege把amr格式文件转化为mp3格式

* @param amrFileName
*/
public static void convertAmrToMp3(String amrFileName,String filepath) {
List<String> commend = new java.util.ArrayList<String>();
commend.add("ffmpeg");
commend.add("-i");
commend.add(filepath + amrFileName);
commend.add(filepath + amrFileName.replace("amr", "mp3"));
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* 压缩图片 文件名为原图名_sub

* @param fileName
* @return
* @throws IOException
*/
public static String createThumbPic(File file,String realPath) throws IOException {
// java.io.File pFile = new java.io.File(file);

String path = realPath+"/"+file.getName().replace(".", "_sub.");
String fileName = file.getName();
java.io.File fo = new java.io.File(path); // 将要转换出的小图文件
int nw = 100;
AffineTransform transform = new AffineTransform();
BufferedImage buffer = ImageIO.read(file); // 读取图片
int width = buffer.getWidth();
int height = buffer.getHeight();
int nh = (nw * height) / width;
double sx = (double) nw / width;
double sy = (double) nh / height;
transform.setToScale(sx, sy);
AffineTransformOp ato = new AffineTransformOp(transform, null);
BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);
ato.filter(buffer, bid);
String type = fileName.substring(fileName.indexOf('.') + 1, fileName.length());
// System.out.println("type = " + type);
ImageIO.write(bid, type, fo);
return fo.getName();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值