`
单眼皮小猪
  • 浏览: 122784 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

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

 
阅读更多
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);
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics