java實現檔案下載的工具類-----------什麼都可以下載,只要傳入response和檔案路徑 兩個參數

來源:互聯網
上載者:User

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;


public class DownLoadUtils {
 public static String getContentType(String fileName) {    
        String fileNameTmp = fileName.toLowerCase();   
        String ret = "";   
        if (fileNameTmp.endsWith("txt")) {     
                ret = "text/plain";    
        }   
        if (fileNameTmp.endsWith("gif")) {    
                ret = "image/gif";  
        } 
         if (fileNameTmp.endsWith("jpg")) {    
                ret = "image/jpeg";    
        }  
         if (fileNameTmp.endsWith("jpeg")) {   
                ret = "image/jpeg";   
        }    
         if (fileNameTmp.endsWith("jpe")) {   
                ret = "image/jpeg";    
        }    
         if (fileNameTmp.endsWith("zip")) { 
                ret = "application/zip";   
        }    
         if (fileNameTmp.endsWith("rar")) {  
                ret = "application/rar";    
        }   
         if (fileNameTmp.endsWith("doc")) { 
                ret = "application/msword";   
        }    
         if (fileNameTmp.endsWith("ppt")) {  
                ret = "application/vnd.ms-powerpoint";   
        }    
         if (fileNameTmp.endsWith("xls")) { 
                ret = "application/vnd.ms-excel";   
        }    
         if (fileNameTmp.endsWith("html")) {    
                ret = "text/html";    
        }   
         if (fileNameTmp.endsWith("htm")) {  
                ret = "text/html";    
        }    
         if (fileNameTmp.endsWith("tif")) { 
                ret = "image/tiff";    
        }   
         if (fileNameTmp.endsWith("tiff")) { 
                ret = "image/tiff";   
        }  
         if (fileNameTmp.endsWith("pdf")) {     
                ret = "application/pdf";   
        }    
         return ret;    
}
//下載部分
  
public static void fileDownLoad(HttpServletResponse response,String filePath) throws ServletException {
 BufferedInputStream bis = null;
 BufferedOutputStream bos = null;
 OutputStream fos = null;
 InputStream fis = null;
 String fileName = filePath.substring(filePath.lastIndexOf("/")+ 1, filePath.length());
 try {
  response.reset();
 response.setContentType(getContentType(fileName));
 response.setHeader("Content-disposition", "attachment;filename="+ new String(fileName.getBytes("gb2312"), "ISO8859-1"));
 fis = new FileInputStream(filePath); 
 bis = new BufferedInputStream(fis);
 fos = response.getOutputStream();
 bos = new BufferedOutputStream(fos);
 int bytesRead = 0;
 byte[] buffer = new byte[5 * 1024];
 while ((bytesRead = bis.read(buffer)) != -1) {
      bos.write(buffer, 0, bytesRead);// 將檔案發送到用戶端 }  
   bos.close();
   bis.close();
   fos.close();
   fis.close();
     }
  } catch (IOException e) {
 response.reset();
 e.printStackTrace();
  } finally {
  try {
   if (fos != null) {
    fos.close();
   }
   if (bos != null) {
    bos.close();
   }
   if (fis != null) {
    fis.close();
   }
   if (bis != null) {
    bis.close(); 
              }
   } catch (IOException e) {
         System.err.print(e);
   }
       }
 } 


}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.