SpringMvc下载

@RequestMapping(value = WebUrlConstant.DOWNLOAD)
	public void download(HttpServletRequest request,HttpServletResponse response,String fileName) {
		OutputStream os = null;
		InputStream is = null;
		try {
			String fileName = new String(fileName.getBytes("iso-8859-1"), "UTF-8");
			String templeteFile = PropertiesUtils.getValue("filePath") + File.separatorChar + fileName;
			response.setContentType("application/octet-stream");
			response.setHeader("content-disposition", "attachment;filename=" + this.getFileName(request, fileName));
			os = response.getOutputStream();
			File file = new File(templeteFile);
			is = new FileInputStream(file);			
			byte[] bt = new byte[(int) file.length()];
			is.read(bt);
			os.write(bt);
			os.flush();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			IOUtils.closeQuietly(os);
			IOUtils.closeQuietly(is);
		}
	}

 

protected String getFileName(HttpServletRequest request, String attachmentName) throws UnsupportedEncodingException {
		String browserName = request.getHeader("User-Agent");
		// IE浏览器
		if (browserName.indexOf("MSIE") != -1) {
			return URLEncoder.encode(attachmentName, "UTF-8");
		} else {
			// 其它浏览器
			return new String(attachmentName.getBytes("UTF-8"), "ISO-8859-1");
		}				 
	}

 

 

你可能感兴趣的