<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 指定所上传文件的总大小不能超过80M......注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="80000000" />
</bean>
public String uploadFile(HttpServletRequest request,
HttpServletResponse response) {
if(request instanceof MultipartHttpServletRequest){
MultipartHttpServletRequest multiReq=(MultipartHttpServletRequest) request;
String orderNum=request.getParameter("orderNum");
String fileType=request.getParameter("fileType");
Map<String, List<MultipartFile>> fileMap=multiReq.getMultiFileMap();
if(fileMap.size()>0){
for(String key:fileMap.keySet()){
List<MultipartFile> list=fileMap.get(key);
if(!list.isEmpty()){
for(int i=0;i<list.size();i++){
MultipartFile webFile= list.get(i);
String channel="web";
String uri=key+".uri";
String uripath=UploadPropertyConfigurer.getProperty(uri);
String realpath=UploadPropertyConfigurer.getProperty("image.realuri")+uripath;
String virtualpath=UploadPropertyConfigurer.getProperty("image.url")+uripath;
String fileName=webFile.getOriginalFilename();
LOGGER.info("filename:"+fileName);
Date date=new Date();
try {
if(fileName.contains(".")){
String [] s=fileName.split("\\.");
fileName=orderNum+"_"+new Date().getTime()+"."+s[1];
}
FileUtils.copyInputStreamToFile(webFile.getInputStream(), new File(realpath, fileName));
OrderFileupload record=new OrderFileupload();
record.setOrderNum(orderNum);
record.setFileuri(virtualpath);
record.setFileType(key);
record.setFilename(fileName);
record.setCreateDate(date);
record.setUploadChannel(channel);
record.setUploadUser("admin");
orderFileuploadMapper.insert(record);
LOGGER.info("上传成功,orderNum:"+orderNum+",fileType:"+request.getParameter("uri")+",fileName:"+fileName);
OrderFileuploadExample example=new OrderFileuploadExample();
example.or().andOrderNumEqualTo(orderNum);
List<OrderFileupload> listfile=orderFileuploadMapper.selectByExample(example);
Map<String,List<String>> map=new HashMap<String,List<String>>();
String ip= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
for(int j=0;j<listfile.size();j++){
OrderFileupload file=listfile.get(j);
if(map.containsKey(file.getFileType())){
map.get(file.getFileType()).add(ip+file.getFileuri()+"/"+file.getFilename());
}else{
List<String> lists=new ArrayList<String>();
lists.add(ip+file.getFileuri()+"/"+file.getFilename());
map.put(file.getFileType(), lists);
} }
ResultDto dto=new ResultDto();
dto.setResultCode(Constant.CODE_ENUM.SUCCESS.code);
dto.setResultDetail(map);
return om.writeValueAsString(dto);
} catch (Exception e) {
File file=new File(realpath, fileName);
if(file.exists()){
FileUtils.deleteQuietly(file);
}
OrderFileuploadExample example=new OrderFileuploadExample();
example.or().andCreateDateEqualTo(date).andOrderNumEqualTo(orderNum);
orderFileuploadMapper.deleteByExample(example);
LOGGER.error("上传异常:",e);
continue;
}
}
}
}
}
LOGGER.info("上传失败,orderNum:"+orderNum+",fileType:"+request.getParameter("uri"));
return "0";
}
return "1"; }