Java Spring文件上传,Java文件上传,Java通用文件上传
================================
©Copyright 蕃薯耀 2018年9月20日
http://fanshuyao.iteye.com/
一、文件上传主代码:
/**
* 通用文件上传
* @param request HttpServletRequest
* @param dirPath String 文件的绝对目录(c:/a/b/)或者相对路径的目录(aa/bb/)
*/
public List<FileBean> uploadFile(HttpServletRequest request, String dirPath){
List<FileBean> files = new ArrayList<FileBean>();
try{
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if(commonsMultipartResolver.isMultipart(request)){
//强制转换需要在xml配置文件上传解析bean:CommonsMultipartResolver,且bean的id必须是multipartResolver
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
@SuppressWarnings("unchecked")
Iterator<String> fileNames = multipartHttpServletRequest.getFileNames();
logger.info("=====dirPath="+ dirPath);
String defaultDir = "H:/scan/ghyw/";
if(!StringUtils.isBlank(dirPath)){
dirPath = dirPath.trim();
if(dirPath.indexOf(":") > -1){
defaultDir = dirPath;
}else{
defaultDir = defaultDir + dirPath;
}
String lastCode = defaultDir.substring(defaultDir.length() - 1);
if(!"/".equals(lastCode)){
defaultDir = defaultDir + "/";
}
File dir = new File(defaultDir);
if(!dir.exists()){
dir.mkdirs();
}
}
logger.info("=====dirPath last="+ defaultDir);
MultipartFile multipartFile = null;
FileBean fileBean = null;
while(fileNames.hasNext()){
multipartFile = multipartHttpServletRequest.getFile(fileNames.next());
String originalName = multipartFile.getOriginalFilename();
if(!StringUtils.isBlank(originalName)){
originalName = new String(originalName.getBytes(), "UTF-8");
}
fileBean = new FileBean();
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
String suffix = originalName.substring(originalName.lastIndexOf("."));
fileBean.setSuffix(suffix);
fileBean.setFileName(uuid + suffix);
fileBean.setOriginalName(originalName);
fileBean.setFileSize(multipartFile.getSize()/1024 + "KB");
fileBean.setFileType(multipartFile.getContentType());
String absolutePath = defaultDir + uuid + suffix;
fileBean.setAbsolutePath(absolutePath);
FileCopyUtils.copy(multipartFile.getBytes(), new FileOutputStream(absolutePath));
files.add(fileBean);
logger.info("=====multipartFile file name = "+ originalName);
logger.info("=====multipartFile file name last = "+ fileBean.getFileName());
}
}
}catch(Exception e){
e.printStackTrace();
logger.info("=====上传文件发生错误:" + e.getMessage());
}
return files;
}
二、文件上传属性Bean
public class FileBean {
/**
* 文件名
*/
private String fileName;
/**
* 原始文件名
*/
private String originalName;
/**
* 文件大小
*/
private String fileSize;
/**
* 文件类型
*/
private String fileType;
/**
* 后缀
*/
private String suffix;
/**
* 绝对路径
*/
private String absolutePath;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getOriginalName() {
return originalName;
}
public void setOriginalName(String originalName) {
this.originalName = originalName;
}
public String getFileSize() {
return fileSize;
}
public void setFileSize(String fileSize) {
this.fileSize = fileSize;
}
public String getFileType() {
return fileType;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
public String getAbsolutePath() {
return absolutePath;
}
public void setAbsolutePath(String absolutePath) {
this.absolutePath = absolutePath;
}
}
三、Action调用方法:
public void fileUpload(HttpServletRequest request, HttpServletResponse response){
Map<String, Object> map = new HashMap<String, Object>();
try{
List<FileBean> fileBeans = this.uploadFile(request, request.getParameter("dirPath"));
map.put("files", fileBeans);
map.put("size", fileBeans == null ? 0: fileBeans.size());
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
writeJson(response, JsonUtils.toJson(map));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Java通用文件下载地址:
http://fanshuyao.iteye.com/blog/2425604
================================
©Copyright 蕃薯耀 2018年9月20日
http://fanshuyao.iteye.com/