好久没写博客,代码小生,
前提是需要导入相应的包,再整合spring+springmvc项目使用
package cn.zhangguimin.web.utils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.web.multipart.MultipartFile;
/**
* 文件工上传具类
*
* @Author 章先森
* @CreateDate 2017年11月11日
* @ www.zhngguimin.cn
*/
public class FilesUpload {
/**
* 上传图片方法
* 图片格式:bmp,jpg,png,gif,jpeg,pneg
* 最大上传大小:size 单位M
* 上传路径:path--request.getSession().getServletContext().getRealPath("static" + File.separator + "pic");
* @param attach
* @param session
* @param request
* @return
* @throws Exception
*/
public String uploadPhoto(MultipartFile attach, HttpSession session, HttpServletRequest request,Integer size) throws Exception {
String idPicPath = null;
if (!attach.isEmpty()) {
String path = request.getSession().getServletContext().getRealPath("static" + File.separator + "pic");
String oldFileName = attach.getOriginalFilename();
String prefix = FilenameUtils.getExtension(oldFileName);
int filesize = size*1024000;
if (attach.getSize() > filesize) {
throw new RuntimeException("SizeError");
// 图片格式:bmp,jpg,png,gif,jpeg,pneg
} else if (prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png")
|| prefix.equalsIgnoreCase("jpeg") || prefix.equalsIgnoreCase("pneg")
|| prefix.equalsIgnoreCase("gif") || prefix.equalsIgnoreCase("bmp")) {
String fileName=System.currentTimeMillis()+RandomUtils.nextInt(1000000)+"user."+prefix;
File targetFile=new File(path,fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
try {
attach.transferTo(targetFile);
} catch (RuntimeException fe) {
fe.printStackTrace();
}
idPicPath=path+File.separator+fileName;
}else {
throw new RuntimeException("PicError");
}
}
return idPicPath;
}
/**
* 多文件上传
* 文件格式:doc,xlsx,zip,txt,docx,pptx
* 最大上传大小:size 单位M
* 上传路径:path--request.getSession().getServletContext().getRealPath("static" + File.separator + "files");
* @param attach
* @param session
* @param request
* @return
* @throws Exception
*/
public List<String> uploadFiles(MultipartFile[] attachs, HttpSession session, HttpServletRequest request,Integer size) throws Exception {
List<String> idPicPath=null;
if (attachs.length>0) {
idPicPath=new ArrayList<>();
for (MultipartFile attach : attachs) {
String path = request.getSession().getServletContext().getRealPath("static" + File.separator + "files");
String oldFileName = attach.getOriginalFilename();
String prefix = FilenameUtils.getExtension(oldFileName);
int filesize = size*1024000;
if (attach.getSize() > filesize) {
throw new RuntimeException("SizeError");
// 图片格式:doc,xlsx,zip,txt,docx,pptx
} else if (prefix.equalsIgnoreCase("doc") || prefix.equalsIgnoreCase("docx")
|| prefix.equalsIgnoreCase("xlsx") || prefix.equalsIgnoreCase("pptx")
|| prefix.equalsIgnoreCase("txt") || prefix.equalsIgnoreCase("zip")) {
String fileName=System.currentTimeMillis()+RandomUtils.nextInt(1000000)+"file."+prefix;
File targetFile=new File(path,fileName);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
try {
attach.transferTo(targetFile);
} catch (RuntimeException fe) {
fe.printStackTrace();
}
idPicPath.add(path+File.separator+fileName);
}else {
throw new RuntimeException("PicError");
}
}
}
return idPicPath;
}
}