当前位置:首页 > 开发 > 编程语言 > Spring > 正文

SpringMvc下载

发表于: 2014-11-05   作者:120153216   来源:转载   浏览:
摘要: @RequestMapping(value = WebUrlConstant.DOWNLOAD) public void download(HttpServletRequest request,HttpServletResponse response,String fileName) { OutputStream os = null; InputStream is = null;
@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");
		}				 
	}

 

 

SpringMvc下载

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
核心代码如下: HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLI
参照了网上相关代码 注意事项: 1 springmvc.xml必须配置: <bean id="multipartResolver" class="o
参照了网上相关代码 注意事项: 1 springmvc.xml必须配置: <bean id="multipartResolver" class="o
参照了网上相关代码 注意事项: 1 springmvc.xml必须配置: <bean id="multipartResolver" class="o
参照了网上相关代码 注意事项: 1 springmvc.xml必须配置: <bean id="multipartResolver" class="o
参照了网上相关代码 注意事项: 1 springmvc.xml必须配置: <bean id="multipartResolver" class="o
参照了网上相关代码 注意事项: 1 springmvc.xml必须配置: Java代码 <bean id="multipartResolver
参照了网上相关代码 注意事项: 1 springmvc.xml必须配置: <bean id="multipartResolver" class="o
参照了网上相关代码 注意事项: 1 springmvc.xml必须配置: <bean id="multipartResolver" class="o
参照了网上相关代码 注意事项: 1 springmvc.xml必须配置: Java代码 <bean id="multipartResolver
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号