springmvc 上传多个文件

/**
	 * 多个文件上传 50*
	 * @param files  <input type="file" name="files"/><input type="file" name="files"/><input type="file" name="files"/>
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/uploadFiles")
    public void uploadFiles(@RequestParam(value = "files", required = false) MultipartFile files[], HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> responseRootMap = new HashMap<String, Object>();
		Map<String, Object> responseMap = new HashMap<String, Object>();
		Map<String, Object> headMap = new HashMap<String, Object>();
		Map<String, Object> bodyMap = new HashMap<String, Object>();
		responseRootMap.put("response", responseMap);
		responseMap.put("head", headMap);
		responseMap.put("body", bodyMap);
		String fileUploadPath = (String) CommonQuartz.commonConfigData().get("img.upload.path");
		response.setContentType("text/json;charset=UTF-8");  
		PrintWriter out = null;
		try {
			out = response.getWriter();
			StringBuilder newFileName = new StringBuilder(100);
			StringBuilder fileUploadPathReally = new StringBuilder();
			if(files!=null){
				List<Map<String,Object>> listUploadResult=new ArrayList<Map<String,Object>>();
				String imgUrlPath=(String) CommonQuartz.commonConfigData().get("img.url");
				Date dateNow=new Date();
				for(MultipartFile file:files){
					newFileName.delete(0, newFileName.length());
					fileUploadPathReally.delete(0, fileUploadPathReally.length());
					String fileName = file.getOriginalFilename();
					// 创建新的文件名字
					newFileName.append(DateUtil.sdfyyyyMMddHHmmssSSS.format(new Date()));
					newFileName.append(fileName);
					logger.info("newFileName:" + newFileName);
					fileUploadPathReally.append(fileUploadPath);
					fileUploadPathReally.append(File.separator);
					fileUploadPathReally.append(newFileName);
					Map<String,Object> mapFileUpload=new HashMap<String, Object>();
					try {
						if(file.getSize()<=IMGMAXSIZE){
							file.transferTo(new File(fileUploadPathReally.toString()));
							String imgUrl=imgUrlPath+newFileName.toString();
							ImgUpload imgUpload=new ImgUpload();
							imgUpload.setImgUrl(imgUrl);
							imgUpload.setNewFileName(newFileName.toString());
							imgUpload.setOriginalFilename(fileName);
							imgUpload.setSavetime(dateNow);
							int insertSize=imgUploadMapper.insert(imgUpload);
							mapFileUpload.put("success", true);
							mapFileUpload.put("originalFilename", fileName);
							mapFileUpload.put("newFilename", newFileName.toString());
							mapFileUpload.put("imgUrl", imgUrl);
							mapFileUpload.put("size", file.getSize());
							mapFileUpload.put("resourceId", imgUpload.getId());
							mapFileUpload.put("insertSize", insertSize);
							listUploadResult.add(mapFileUpload);
						}else{
							mapFileUpload.put("success", false);
							mapFileUpload.put("originalFilename", fileName);
							mapFileUpload.put("size", file.getSize());
							mapFileUpload.put("message", "文件超过5MB");
							mapFileUpload.put("allowMaxSize", IMGMAXSIZE);
							listUploadResult.add(mapFileUpload);
						}
					} catch (IllegalStateException e) {
						mapFileUpload.put("success", false);
						mapFileUpload.put("originalFilename", fileName);
						mapFileUpload.put("size", file.getSize());
						listUploadResult.add(mapFileUpload);
						logger.error(fileName+"传输上传失败",e);
					} catch (IOException e) {
						mapFileUpload.put("success", false);
						mapFileUpload.put("originalFilename", fileName);
						mapFileUpload.put("size", file.getSize());
						listUploadResult.add(mapFileUpload);
						logger.error(fileName+"传输上传失败",e);
					}
				}
				//将长传结果集放到结果中
				bodyMap.put("uploadFilesResult", listUploadResult);
				bodyMap.put("success", true);
				bodyMap.put("message", "上传文件个数为"+files.length);
			}else{
				bodyMap.put("success", false);
				bodyMap.put("message", "上传文件个数为0");
			}
			headMap.put("desc", "上传成功");
			headMap.put("status", "0");
			bodyMap.put("sysTime", DateUtil.sdfyyyy_MM_dd_HH_mm_ss.format(new Date()));
			//bodyMap.put("sessionId", "");
			out.write(JSONObject.fromObject(responseRootMap).toString());  
			out.flush(); 
			logger.info(JSONObject.fromObject(responseRootMap).toString());
		} catch (Exception e) {
			headMap.put("desc", "系统内部错误");
			headMap.put("status", "5001");
			bodyMap.put("sysTime", DateUtil.sdfyyyy_MM_dd_HH_mm_ss.format(new Date()));
			//bodyMap.put("sessionId", "");
			logger.error("上传图片失败",e);
			out.write(JSONObject.fromObject(responseRootMap).toString());  
			out.flush(); 
		}
    }

猜你喜欢

转载自blog.csdn.net/ctllin/article/details/78971756