SpringMvc多文件上傳簡單實現,springmvc文件上傳
public ResponseItem uploadFile(MultipartHttpServletRequest request,FileItem fileItem,PageData pd) {
ResponseItem responseItem=new ResponseItem();
try {
boolean isSuccess = false;
MultiValueMap<String,MultipartFile> multiMap = request.getMultiFileMap();
String relativePath = CreateDateDirUtil.create(Const.UPLOADPATH+"/upload");//保存文件夾
Set<String> keys = multiMap.keySet();//遍歷文件
isSuccess=true;
if(isSuccess){
FileItem uploadfile = new FileItem();
for (String key:keys) {
List<MultipartFile> mutiFiles = multiMap.get(key);
for(MultipartFile file :mutiFiles){
String uuid= UuidUtil.get32UUID();
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."));//獲取文件類型
uploadfile.setProc(fileItem.getProc());//設置流程實例ID
uploadfile.setFileTempname(uuid+suffix);//設置數據庫文件存儲名,32位
uploadfile.setNames(fileItem.getNames());//設置上傳者姓名
uploadfile.setUserid(fileItem.getUserid());//設置上傳者id
logger.debug("==>"+suffix);
String fileName = URLDecoder.decode(file.getOriginalFilename());
logger.debug("上傳附件名==>"+fileName);
uploadfile.setFilename(fileName);//上傳附件名
uploadfile.setFilepath("upload/"+relativePath);
String type=fileItem.getType();
if(type.equals("blfj")){
fileItem.setType("辦理附件");
}
if(type.equals("jxfj")){
fileItem.setType("結項附件");
}
if(type.equals("xcfj")){
fileItem.setType("巡查附件");
}
uploadfile.setType("辦理附件");
uploadfile.setTime(new Date());//上傳時間
logger.debug("==>"+Const.UPLOADPATH+"/"+relativePath+uuid+suffix);
file.transferTo(new File(Const.UPLOADPATH+"/upload/"+relativePath,uuid+suffix));//寫入文件,文件名通過UUID生產
/** 插入媒體文件 */
isSuccess = workflowDao.insertWorkflowUploadFile(uploadfile);
}
}
if(isSuccess){
return responseItem.getSuccessInfo(isSuccess);
}else{
return responseItem.getFailureInfo("false");
}
}
}catch (Exception e){
e.printStackTrace();
}
return responseItem;
}