1、pom.xml
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
2、代码
/**
* 上传图片
*/
@RequestMapping(method = RequestMethod.POST, value = "upload")
public Object UploadFile(MultipartHttpServletRequest requestFile) throws IllegalStateException, IOException {
log.info("*******************进入图片上传的方法*************************");
ServletContext servletContext = WebContext.getServletContext();
// 得到文件绝对路径
String realPath = servletContext.getRealPath("/");
System.out.println("realPath:"+realPath);
InputStream is = null;
FileOutputStream fs = null;
DataState dataState = new DataState();
HttpServletRequest request = WebContext.getServletRequest();
UserEntity user = WebContext.getUserEntity();
Iterator<String> itr = requestFile.getFileNames();
requestFile.setCharacterEncoding("utf-8");
MultipartFile file = requestFile.getFile(itr.next());
// 转换成File
CommonsMultipartFile cf = (CommonsMultipartFile) file;
DiskFileItem fi = (DiskFileItem) cf.getFileItem();
File f = fi.getStoreLocation();
String fileName = file.getOriginalFilename();
System.out.println(fileName);
// 自定义的文件名称
String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
String totrueFileName = String.valueOf(System.currentTimeMillis())+1 + fileName;
String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
/** 临时文件夹*/
String imgPath = "uploadImgTemp" + File.separator;
String tempPath = realPath +"\\"+ imgPath;
System.out.println("old-path-" + tempPath);
File oldFile = new File(tempPath);
if (!oldFile.exists()) {
oldFile.mkdirs();
}
/** 处理后文件夹*/
String newImaPath = "uploadImg" + File.separator;
String newPath = realPath +"\\" + newImaPath;
System.out.println("new-path-" + newPath);
File newFile = new File(newPath);
if (!newFile.exists()) {
newFile.mkdirs();
}
/** 先存取源文件*/
is = file.getInputStream();
fs = new FileOutputStream(tempPath + trueFileName);
//...
if (file.getSize() > 0 && file.getSize()>1048576) {
byte[] buffer = new byte[1024 * 1024];
int bytesum = 0;
int byteread = 0;
while ((byteread = is.read(buffer)) != -1) {
bytesum += byteread;
fs.write(buffer, 0, byteread);
fs.flush();
}
fs.close();
is.close();
}
/** 处理源文件 ,进行压缩再放置到新的文件夹*/
String oldPath = tempPath + trueFileName;
String copyPath = newPath + totrueFileName;
Thumbnails.of(oldPath).scale(0.2f).toFile(copyPath);//按比例缩小
File toFile = new File(copyPath);
log.info("*******************上传图片大小:"+toFile.length()+"*************************");
//ftp上传
String filePath = Common.getValue("FTP_REMOTE_FILEPATH", "com.ztb.common.util.properties.ftp") + time;
boolean result = FtpUtil.upload(new File(copyPath), filePath, totrueFileName);
if (result == false) {
dataState.setStateCode(StateCode.OPT_ERROR);
dataState.setData("");
return DataEvent.wrap("uploadok", dataState);
}
String url = Common.getValue("RETURN_URL", "com.ztb.common.util.properties.ftp") + time + "/" + totrueFileName;
WebContext.getSession().setAttribute("url", url);
dataState.setStateCode(StateCode.OPT_OK);
dataState.setData(url);
return DataEvent.wrap("uploadok", dataState);
}