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

springMVC下载+文件名乱码+从配置文件读取路径

发表于: 2014-02-24   作者:单眼皮小猪   来源:转载   浏览:
摘要: public ModelAndView download(HttpServletRequest request,HttpServletResponse response) throws Exception { String fileName = request.getParameter("fileName"); String filePath = r
public ModelAndView download(HttpServletRequest request,HttpServletResponse response) throws Exception {
	     String fileName = request.getParameter("fileName");
	     String filePath = readProperties("report.path.prefix")+"/supplier/"+fileName;
	     File file = new File(filePath);
	     // 清空response 
        response.reset(); 
        response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("gbk"),"iso-8859-1") + "\"");  
        response.addHeader("Content-Length", "" + file.length());  
        response.setContentType("application/octet-stream;charset=UTF-8");  
        InputStream in = null;
        OutputStream toClient = null;
        try{ 
	         //以流的形式下载文件 
        	in = new BufferedInputStream(new FileInputStream(filePath)); 
	         byte[] buffer = new byte[in.available()]; 
	         in.read(buffer); 
	         in.close();
	         
	         toClient = new BufferedOutputStream(response.getOutputStream()); 
	         toClient.write(buffer); 
	         toClient.flush(); 
	         
	     }catch(Exception e){ 
	         e.printStackTrace(); 
	     }finally{
	    	 toClient.close(); 
	     }
	     
	     return null;
	}
	public static Properties proper= null;
	private String readProperties(String key){
		if(this.proper==null){
			 InputStream in = this.getClass().getResourceAsStream("/env.properties");
			 try {
				 this.proper = new Properties();
				 this.proper.load(in);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return this.proper.getProperty(key);
	}

springMVC下载+文件名乱码+从配置文件读取路径

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号