struts下载

来源:互联网 发布:数据噪声是什么意思 编辑:程序博客网 时间:2024/04/29 13:49

String fileName = request.getParameter("fileName");
  
  String folderPath = this.getServlet().getServletContext().getRealPath("/download");
  
  String fileRealPath = folderPath +"/"+ fileName;
  
  
  
  
  
  try {
   response.setContentType("bin");
   response.addHeader("Content-Disposition", "attachment;filename=/""+URLEncoder.encode(fileName, "utf-8")+"/"");
   InputStream is = new FileInputStream(fileRealPath);
   
   OutputStream os =  response.getOutputStream();
   
   byte[] b = new byte[1024];
   int len = 0;
   
   while((len = is.read(b))>0){
    os.write(b,0,len);
   }
   
   
   is.close();
   os.flush();
   os.close();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }