spring mvc 文件上传

spring mvc 文件上传:

@RequestMapping(value="/doUploadFile.do")
	@ResponseBody
	public Map<String, Object> doUploadFile(UpdateVO obj,
			HttpServletRequest request,HttpServletResponse response) throws Exception{

		log.info("start update file..");
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;  
	    Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); 
	    
	    List outList = new ArrayList();
	    Iterator<?> itr = fileMap.entrySet().iterator();
	    FileInfo info = new FileInfo();
	    while(itr.hasNext()){
		   Entry<String, MultipartFile> ety = (Entry<String, MultipartFile>) itr.next();
		   MultipartFile file = ety.getValue();
		   
		   String keyPrefix = "";
		   if(obj.getUploadType().equals("firmware")){
			   keyPrefix = Constants.FOLDER_FIRMWARE + "/" + obj.getProductKey()
						+ "/" + obj.getModelId() + "/" + obj.getFirmwareVersion() + "/";
		   }
		   else if(obj.getUploadType().equals("cameraApp")){
			   keyPrefix = Constants.FOLDER_CAMERAAPP + "/" + obj.getProductKey()
						+ "/" + obj.getModelId() + "/" + obj.getCameraAppVersion() + "/";
		   }
		   else if(obj.getUploadType().equals("client")){
			   keyPrefix = Constants.FOLDER_CLIENT + "/" + obj.getProductKey()
						+ "/" + obj.getClientOS() + "/" + obj.getClientVersion() + "/";
		   }
		   
		   log.info("enter upload File function");
		   info = uploadFile(file, request,keyPrefix);
	    }
	    
		ResponseResult result= new ResponseResult();
		result.setSuccess(true);
		result.setReturnObject("result",JSONObject.fromObject(info).toString());
		return result.returnResult();
	}

/***inner method***/
	
	private FileInfo uploadFile(MultipartFile file,HttpServletRequest request,String keyPrefix) throws Exception {
		
		String dir = request.getSession().getServletContext().getRealPath("/")+ Constants.UPLOADDIR;
		
		log.info("upload file dir :{}",dir);
		FileInfo info = new FileInfo();
		if (!file.isEmpty()) {
			// upload file to server first
			String uploadDir = dir +  "/" + keyPrefix;
			File dirFile = new File(uploadDir);
			if (!dirFile.exists()) {
				log.info("create  local file  :{}",uploadDir);
				dirFile.mkdirs();
			}else{
			}
		
			String filename = file.getOriginalFilename();
			File uploadFile = new File(dirFile, filename);

			InputStream is = file.getInputStream();
			FileOutputStream fos = new FileOutputStream(uploadFile);
			
			int size = is.available();
			byte[] fields = new byte[size];
			int len = is.read(fields);
			String md5 = null;
			if(len != -1) {
				if (!isAws() && (len / Constants.PART_SIZE != 0)) {
					md5 = ComputeMessageDigest.computeDigest(fields, "MD5");
				}
				fos.write(fields, 0, len);
			}
			
			is.close();
			fos.flush();
			fos.close();

			String key = keyPrefix + filename;
			
			log.info("start update to s3 :{}", key);
			if(isAws()){
				info = AWSUtil.uploadFile(updateService.getS3Param().getS3Param(),uploadFile, key);
			}else{
				info = AliYunUtil.uploadFile(updateService.getOSSParam().getOssParam(),uploadFile, key, md5);
			}
			
			log.info("Upload file to Cloud successful");
			
			uploadFile.delete();
		}
		return info;
	}











你可能感兴趣的:(spring mvc 文件上传)