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

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

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

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

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

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

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

2016525184904951.png (333×385)

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

2016525185001047.png (1366×702)

但是這里會(huì)出現(xiàn)一個(gè)問(wèn)題,就是單擊下載壓縮包的時(shí)候會(huì)彈出下載頁(yè)面,但是下載圖片的時(shí)候?yàn)g覽器就直接打開(kāi)了圖片,沒(méi)有下載。

2016525185028779.png (438×330)

這是因?yàn)橥ㄟ^(guò)超鏈接下載文件時(shí),如果瀏覽器可以識(shí)別該文件格式,瀏覽器就會(huì)直接打開(kāi)。只有瀏覽器不能識(shí)別該文件格式的時(shí)候,才會(huì)實(shí)現(xiàn)下載。因此利用第二種方法實(shí)現(xiàn)下載功能。

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

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

其中,/day06/ServletDownload 是servlet程序的映射路徑
然后新建一個(gè)servlet,名稱(chēng)為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 
     
    //獲得請(qǐng)求文件名 
    String filename = request.getParameter("filename"); 
    System.out.println(filename); 
     
    //設(shè)置文件MIME類(lèi)型 
    response.setContentType(getServletContext().getMimeType(filename)); 
    //設(shè)置Content-Disposition 
    response.setHeader("Content-Disposition", "attachment;filename="+filename); 
    //讀取目標(biāo)文件,通過(guò)response將目標(biāo)文件寫(xiě)到客戶端 
    //獲取目標(biāo)文件的絕對(duì)路徑 
    String fullFileName = getServletContext().getRealPath("/download/" + filename); 
    //System.out.println(fullFileName); 
    //讀取文件 
    InputStream in = new FileInputStream(fullFileName); 
    OutputStream out = response.getOutputStream(); 
     
    //寫(xiě)文件 
    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ù)器,即可實(shí)現(xiàn)對(duì)壓縮包和對(duì)圖片的下載。

2016525185126430.png (438×330)

2016525185151269.png (438×330)

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

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

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

String filePath = this.getClass().getClassLoader().getResource("").toURI().getPath()
          + "/exportPdf.pdf"; //文件在項(xiàng)目中的路徑
    File outfile = new File(filePath);
    String filename = outfile.getName();// 獲取文件名稱(chēng)
    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ù)種類(lèi)
    //獲取返回體輸出權(quán)
    OutputStream os = new BufferedOutputStream(response.getOutputStream()); 
    os.write(buffer); // 輸出文件
    os.flush();
    os.close();

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

相關(guān)文章

最新評(píng)論