欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java Web項目中實現(xiàn)文件下載功能的實例教程

 更新時間:2016年05月25日 18:55:56   作者:longshengguoji  
這篇文章主要介紹了Java Web項目中實現(xiàn)文件下載功能的實例教程,分別講解了通過超鏈接實現(xiàn)下載以及通過Servlet程序?qū)崿F(xiàn)下載的方式,需要的朋友可以參考下

需求:實現(xiàn)一個具有文件下載功能的網(wǎng)頁,主要下載壓縮包和圖片
兩種實現(xiàn)方法:

一:通過超鏈接實現(xiàn)下載
在HTML網(wǎng)頁中,通過超鏈接鏈接到要下載的文件的地址

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
<h1>通過鏈接下載文件</h1> 
<a href="/day06/download/cors.zip">壓縮包</a> 
<a href="/day06/download/1.png">圖片</a> 
</body> 
</html> 

其中day06/download是文檔路徑,本實例的程序結(jié)構(gòu)如下:

2016525184904951.png (333×385)

程序運(yùn)行后,可以通過單擊需要下載文檔實現(xiàn)下載

2016525185001047.png (1366×702)

但是這里會出現(xiàn)一個問題,就是單擊下載壓縮包的時候會彈出下載頁面,但是下載圖片的時候瀏覽器就直接打開了圖片,沒有下載。

2016525185028779.png (438×330)

這是因為通過超鏈接下載文件時,如果瀏覽器可以識別該文件格式,瀏覽器就會直接打開。只有瀏覽器不能識別該文件格式的時候,才會實現(xiàn)下載。因此利用第二種方法實現(xiàn)下載功能。

二:通過Servlet程序?qū)崿F(xiàn)下載
通過Servlet下載文件的原理是通過servlet讀取目標(biāo)程序,將資源返回客戶端。

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
<h1>通過鏈接下載文件</h1> 
<a href="/day06/download/cors.zip">壓縮包</a> 
<a href="/day06/download/1.png">圖片</a> 
<h1>通過servlet程序下載文件</h1> 
<a href="/day06/ServletDownload?filename=cors.zip">壓縮包</a> 
<a href="/day06/ServletDownload?filename=1.png">圖片</a> 
</body> 
</html> 

其中,/day06/ServletDownload 是servlet程序的映射路徑
然后新建一個servlet,名稱為ServletDownload,URL映射為/ServletDownload

2016525185054838.png (525×524)

添加代碼如下:

package com.lsgjzhuwei.servlet.response; 
 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
/** 
 * Servlet implementation class ServletDownload 
 */ 
@WebServlet(asyncSupported = true, urlPatterns = { "/ServletDownload" }) 
public class ServletDownload extends HttpServlet { 
  private static final long serialVersionUID = 1L; 
     
  /** 
   * @see HttpServlet#HttpServlet() 
   */ 
  public ServletDownload() { 
    super(); 
    // TODO Auto-generated constructor stub 
  } 
 
  /** 
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
   */ 
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
     
    //獲得請求文件名 
    String filename = request.getParameter("filename"); 
    System.out.println(filename); 
     
    //設(shè)置文件MIME類型 
    response.setContentType(getServletContext().getMimeType(filename)); 
    //設(shè)置Content-Disposition 
    response.setHeader("Content-Disposition", "attachment;filename="+filename); 
    //讀取目標(biāo)文件,通過response將目標(biāo)文件寫到客戶端 
    //獲取目標(biāo)文件的絕對路徑 
    String fullFileName = getServletContext().getRealPath("/download/" + filename); 
    //System.out.println(fullFileName); 
    //讀取文件 
    InputStream in = new FileInputStream(fullFileName); 
    OutputStream out = response.getOutputStream(); 
     
    //寫文件 
    int b; 
    while((b=in.read())!= -1) 
    { 
      out.write(b); 
    } 
     
    in.close(); 
    out.close(); 
  } 
 
  /** 
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
   */ 
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
  } 
 
} 

重啟tomcat服務(wù)器,即可實現(xiàn)對壓縮包和對圖片的下載。

2016525185126430.png (438×330)

2016525185151269.png (438×330)

三、小技巧:
點擊鏈接來下載文件的方式很簡便,后臺把文件流輸出來,通過瀏覽器實現(xiàn)下載功能,包括詢問位置與文件存放,大多數(shù)瀏覽器會配置一個固定位置,不一定每次都問。

前端就非常簡單了,一個<a>標(biāo)簽,href=“后臺方法地址”,如果你的需求不能直接用超鏈接方式,可以在js里寫 window.location.href =“后臺方法地址”。

這樣跳轉(zhuǎn)到后臺方法后

String filePath = this.getClass().getClassLoader().getResource("").toURI().getPath()
          + "/exportPdf.pdf"; //文件在項目中的路徑
    File outfile = new File(filePath);
    String filename = outfile.getName();// 獲取文件名稱
    InputStream fis = new BufferedInputStream(new FileInputStream(
          filePath));
    byte[] buffer = new byte[fis.available()]; 
    fis.read(buffer); //讀取文件流
    fis.close();
    response.reset(); //重置結(jié)果集
    response.addHeader("Content-Disposition", "attachment;filename="
        + new String(filename.replaceAll(" ", "").getBytes("utf-8"),
        "iso8859-1")); //返回頭 文件名
    response.addHeader("Content-Length", "" + outfile.length()); //返回頭 文件大小
    response.setContentType("application/octet-stream");  //設(shè)置數(shù)據(jù)種類
    //獲取返回體輸出權(quán)
    OutputStream os = new BufferedOutputStream(response.getOutputStream()); 
    os.write(buffer); // 輸出文件
    os.flush();
    os.close();

瀏覽器會直接識別這種形式的文件輸出,彈出對話框。
注意此方法一定要用鏈接方式調(diào)后臺,使用ajax和XMLHttpRequest方式都是不行的,這樣返回的文件流會返回到方法的回調(diào)函數(shù)中,當(dāng)然如果你想在js中獲取文件,這樣也行。

相關(guān)文章

最新評論