`

spring mvc 上传(ajaxfileupload.js)、下载

 
阅读更多
@RequestMapping(value = "/uploadt", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addCaseResult(HttpServletRequest request,
			CaseResult caseResult,
			@RequestParam(value = "file", required = false) MultipartFile file) {
		String filePath = "";
		if (file != null && !file.isEmpty()) {
			String orName = file.getOriginalFilename();
			String guid = request.getParameter("guid");
			filePath = fileUpladPath + "/" + guid + "_" + orName;
			File outFile = new File(filePath);
			try {
				file.transferTo(outFile);
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
		}
		ajax.setSuccess(true);
		ajax.setMsg("上传成功");
		ObjectMapper om = new ObjectMapper();
		try {
			return om.writeValueAsString(ajax);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return "";
		}

	}

 

spring 配置

<!--  这里申明的id必须为multipartResolver  -->  
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="100000" />
	</bean>

 

@RequestMapping(value = "/fileIsExists")
	@ResponseBody
	public AjaxJson fileIsExists(
			@RequestParam(value = "filePath", required = false) String filePath) {
		AjaxJson ajax = new AjaxJson();
		if (new File(filePath).exists()) {
			ajax.setSuccess(true);
		} else {
			ajax.setSuccess(false);
			ajax.setMsg("附件不存在");
		}
		return ajax;
	}
	/**
	 * 下载附件
	 */
	@RequestMapping("downloadFile")
	public ModelAndView downloadFile(
			@RequestParam(value = "filePath", required = false) String filePath,
			HttpServletRequest request, HttpServletResponse resp) {
		resp.setContentType("application/x-msdownload;");
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			logger.error(e.getMessage());
		}
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		File file = new File(filePath);
		if (file.exists()) {
			try {
				long fileLength = file.length();
				filePath = filePath.replace("\\", "/");
				String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
				resp.setContentType("application/x-msdownload;");  
				resp.setHeader("Content-Disposition", "attachment;filename="
						+ new String(fileName.getBytes("utf-8"), "ISO8859-1"));  
				resp.setHeader("Content-Length", String.valueOf(fileLength));
				bis = new BufferedInputStream(new FileInputStream(filePath));
				bos = new BufferedOutputStream(resp.getOutputStream());
				byte[] buff = new byte[2048];
				int byteRead;
				while (-1 != (byteRead = bis.read(buff, 0, buff.length))) {
					bos.write(buff, 0, byteRead);
				}
			} catch (Exception e) {
				logger.error(e.getMessage());
			} finally {
				closeStream(bis, bos);
			}
		}
		return null;
	}

	private void closeStream(BufferedInputStream bis, BufferedOutputStream bos) {
		try {
			if (bis != null) {
				bis.close();
			}
			if (bos != null) {
				bos.close();
			}
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}

 

var dataInfo = { cases: cases, description: description,password:password,insertUser:insertUser,fromUser:fromUser} ;
	$.ajaxFileUpload({  
			type : "POST",	
			secureuri :false,
			url : '<%=request.getContextPath()%>/ws/caseResult/addCaseResult?guid='+guid,
			data : dataInfo,
			dataType: 'json', 
       		fileElementId: 'file',      
       		error : function(data, status, e) {
       			alert(data.message);      
       		},       
       		success : function(data, status) {
       			var msg = data.msg;
       			var success = data.success;
       			if(success){
       				alert(msg); 
       				window.location.href="<%=request.getContextPath()%>/ws/caseResult/list";
       			}else{
       				alert(msg);
       			}      
       		}       
 	}); 

 

<form name="form" id="caseResultForm" method="post" enctype="multipart/form-data">
<li class="name_width">附件:</li>
					<li class="reg_input"><input type="file" name="file" id="file"/></li>
</form>

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics