* 文件上传
* @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();
}
}