文件上传有多种工具类可以选择,比如:conmons-FileUpload.jar , springMVC自带的文件上传工具类。在多种工具类里本人觉得transferTo()方法最简便好用。下面是基于它实现的图片上传功能的代码。当然它不仅只限于图片格式。
package com.jjs.app.web.controller;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.mljr.carfinance.crm.app.util.FileUtils;
import com.mljr.carfinance.crm.app.util.Logs;
import com.mljr.carfinance.crm.app.util.RespMsg;
/**
* 图片上传下载类
* @author jjs
*
*/
@RestController
@RequestMapping("/img")
public class UploadImgController extends BaseController{
@Value("${img.upload.dir}")
private static String imgUploadDir;// 图片上传路径
/**
* 图片上传
* @param imgFile
* @return RespMsg
* @throws IllegalStateException
* @throws IOException
*/
@RequestMapping("/upload")
@ResponseBody
public RespMsg upLoad(@RequestParam("imgFile") MultipartFile imgFile) throws IllegalStateException, IOException{
if(imgFile != null){
String imgName = imgFile.getOriginalFilename();
String ext = imgName.substring(imgName.lastIndexOf(".") +1);
if(ext.equals("jpg") || ext.equals("png") || ext.equals("jpeg")){
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
String dir = sf.format(new Date());
File dayDir = new File(imgUploadDir + File.separator + dir);
sf.applyPattern("yyyyMMddHHmmssSSS");
String newImgName = sf.format(new Date()) + "_" + new Random().nextInt(1000) + "." + ext;
String savePath = dayDir + File.separator + newImgName;
imgFile.transferTo(FileUtils.createFile(new File(savePath)));
Logs.C.info("图片'"+ imgName +"'上传成功", savePath);
return success("上传成功");
}
}
return fail("上传失败");
}
/**
* 多图片同时上传
* @param imgFile
* @return RespMsg
* @throws IllegalStateException
* @throws IOException
*/
@RequestMapping("/multiUpload")
@ResponseBody
public RespMsg upLoad(@RequestParam("imgFile") CommonsMultipartFile[] imgFiles) throws IllegalStateException, IOException{
if(imgFiles.length != 0){
for(int i = 0; i < imgFiles.length; i++){
if(imgFiles[i] != null && imgFiles[i].getSize()!=0){
String imgName = imgFiles[i].getOriginalFilename();
String ext = imgName.substring(imgName.lastIndexOf(".") +1);
if(ext.equals("jpg") || ext.equals("png") || ext.equals("jpeg")){
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
String dir = sf.format(new Date());
File dayDir = new File(imgUploadDir + File.separator + dir);
sf.applyPattern("yyyyMMddHHmmssSSS");
String newImgName = sf.format(new Date()) + "_" + new Random().nextInt(1000) + "." + ext;
String savePath = dayDir + File.separator + newImgName;
imgFiles[i].transferTo(FileUtils.createFile(new File(savePath)));
Logs.C.info("图片'"+ imgName +"'上传成功", savePath);
}
}
}
return success("上传成功");
}
return fail("上传失败");
}
}
工具类:
package com.jjs.app.util;
import java.io.File;
import java.io.IOException;
/**
* 文件工具类
* @author jjs
*
*/
public class FileUtils {
public static File createFile(File file) throws IOException {
if (!file.exists()) {
makeDir(file.getParentFile());
}
file.createNewFile();
return file;
}
public static void makeDir(File dir) {
if (!dir.getParentFile().exists()) {
makeDir(dir.getParentFile());
}
dir.mkdir();
}
}