文件下载

 

  
  
  
  
  1. //这种方式会有bug  
  2. /*public String download() throws Exception{  
  3.         String path=getPath();  
  4.         fileNamefileName=fileName.substring(0,4)+"年"+fileName.substring(5,7)+"月"+fileName.substring(8,10)+"日 "+fileName.substring(11,13)+"时"+fileName.substring(14, 16)+"分"+fileName.substring(16,fileName.length());  
  5.         fileName=new String(fileName.getBytes(),"iso8859-1");  
  6.         File file=new File(path,fileName);  
  7.         ServletActionContext.getResponse().setHeader("Content-disposition", "attachment;filename="+fileName);  
  8.         FileInputStream fis=new FileInputStream(file);  
  9.         byte[] buf=new byte[1024];  
  10.         OutputStream os=ServletActionContext.getResponse().getOutputStream();  
  11.         int n=-1;  
  12.         while((n=fis.read(buf))!=-1){  
  13.             os.write(buf, 0, n);  
  14.         }  
  15.         os.close();  
  16.         fis.close();  
  17.         return null;  
  18.     }*/  
  19.  
  20. //此方式解决上面方式的bug  
  21. public String download() throws Exception{  
  22.         HttpServletResponse response = ServletActionContext.getResponse();  
  23.         HttpServletRequest request = ServletActionContext.getRequest();   
  24.         response.setContentType("text/html;charset=utf-8");  
  25.         String filePath = getPath();  
  26.           
  27.         BufferedInputStream bis = null;  
  28.         OutputStream out = null;  
  29.         if(StringUtils.isNotBlank(filePath)){  
  30.              try {  
  31. //               filePath = "D:\\expDataBackUp\\aa_2013年02月20日11时43分37秒.dmp";  
  32.                  fileNamefileName=fileName.substring(0,4)+"年"+fileName.substring(5,7)+"月"+fileName.substring(8,10)+"日 "+fileName.substring(11,13)+"时"+fileName.substring(14, 16)+"分"+fileName.substring(16,fileName.length());  
  33.                  bis = new BufferedInputStream(new FileInputStream(filePath+"\\\\"+fileName));  
  34.                     response.reset();  
  35.                     response.setContentType("application/x-download");  
  36.                     //String fileName = new String(filePath.substring(filePath.lastIndexOf("\\")+1, filePath.length()).getBytes(), "iso8859-1");  
  37.                     fileName=new String(fileName.getBytes(),"iso8859-1");  
  38.                     response.addHeader("Content-Disposition", "attachment;filename="+ fileName);  
  39.                     out = response.getOutputStream();  
  40.                     byte[] buffer = new byte[1024];   
  41.                     int size = 0;  
  42.                     while ((size = bis.read(buffer, 0, buffer.length)) != -1) {  
  43.                         out.write(buffer, 0, size);  
  44.                     }  
  45.                       
  46.              } catch (Exception e) {  
  47.                 e.printStackTrace();  
  48.              }finally{//关闭输入输出流  
  49.                 try {  
  50.                     if(out != null)out.close();  
  51.                     if(bis != null)bis.close();  
  52.                 } catch (IOException e) {  
  53.                     e.printStackTrace();  
  54.                 }  
  55.              }  
  56.         }else{  
  57.         }  
  58.         return null;  
  59.     } 

 

你可能感兴趣的:(文件下载)