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;
}
转载于:https://www.cnblogs.com/tzt1995/p/5980845.html