最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaEE實現(xiàn)文件下載

 更新時間:2014年10月26日 11:01:47   投稿:hebedich  
這篇文章主要介紹了JavaEE實現(xiàn)文件下載的方法,非常的實用,需要的朋友可以參考下

 我們先來看一個最簡單的文件下載的例子:

復(fù)制代碼 代碼如下:

 package com.yyz.response;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 //文件下載
 public class ResponseDemo extends HttpServlet {
     public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
         String realpath = this.getServletContext().getRealPath("/download/1.gif");
         String filename = realpath.substring(realpath.lastIndexOf("\\")+1);
         response.setHeader("content-disposition", "attachment;filename="+filename);
         //服務(wù)器通過這個頭,告訴瀏覽器以下載方式打開數(shù)據(jù)
         FileInputStream in = new FileInputStream(realpath);
         int len = 0;
         byte buffer[]=new byte[1024];
         OutputStream out = response.getOutputStream();
         while((len = in.read(buffer))>0){
             out.write(buffer, 0, len);
             }
         in.close();
         //out不用close,response在銷毀的時候服務(wù)器會自動關(guān)閉與response相關(guān)的流。
 }
     public void doPost(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
            doGet(request,response);
     }
 }

 該段代碼的功能是從服務(wù)器端下載圖片1.png。目錄結(jié)構(gòu)用MyEclipse的package explorer顯示如下:

  讓我們增加一點難度,我們要下載的文件是一個中文名字的文件。由于在http協(xié)議中頭文件中的東西只能是ASCII字符,因而通過上述方式(直接將  String realpath = this.getServletContext().getRealPath("/download/1.gif");改為  String realpath = this.getServletContext().getRealPath("/download/圖片.gif");)直接拿文件,

會出現(xiàn)亂碼問題。附上測試結(jié)果:

  要解決這個問題,要用到 URLEncoder類的encode方法:

復(fù)制代碼 代碼如下:

 package com.yyz.response;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.net.URLEncoder;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 //中文文件下載時,中文文件名要經(jīng)過URL編碼。
 public class ResponseDemo extends HttpServlet {
     public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
         String realpath = this.getServletContext().getRealPath("/download/圖片.gif");
         String filename = realpath.substring(realpath.lastIndexOf("\\")+1);
         response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename,"UTF-8"));
        //本函數(shù)將字符串以 URL 編碼
         FileInputStream in = new FileInputStream(realpath);
         int len = 0;
         byte buffer[]=new byte[1024];
         OutputStream out = response.getOutputStream();
         while((len = in.read(buffer))>0){
             out.write(buffer, 0, len);
             }
         in.close();
 }
     public void doPost(HttpServletRequest request, HttpServletResponse response)
             throws ServletException, IOException {
            doGet(request,response);
     }
 }

 附上測試結(jié)果:

    另外這里有一個小細(xì)節(jié)需要大家注意:

   不能用FileReader代替FileInputStream。用FileReader會丟失數(shù)據(jù),原因是這樣的:FileReader是字符流,而圖片,媒體文件等的數(shù)據(jù)都是以01的方式存儲,用FileReader讀的時候需要查閱一個編碼表,如果未指定一種編碼,則使用相應(yīng)平臺的默認(rèn)編碼。如在中國的電腦就會去查GB2312。當(dāng)讀到GB2312碼表中不存在的編碼時,會將該數(shù)據(jù)編碼成'?',結(jié)束后數(shù)據(jù)就變成中文和'?'的混合。發(fā)到客戶端后顯示時再次查閱碼表,將所有的'?'替換成'?'的編碼,就會丟失數(shù)據(jù)。向這種細(xì)節(jié)只需要記住一點:字節(jié)流可以處理任意類型的數(shù)據(jù),字符流只能處理字符數(shù)據(jù)。

相關(guān)文章

最新評論

澳门| 巧家县| 富阳市| 嘉定区| 梧州市| 沁源县| 河南省| 钟山县| 北海市| 恩平市| 仙游县| 花莲县| 子洲县| 鹤岗市| 宣城市| 新沂市| 孝感市| 出国| 高邮市| 钟祥市| 樟树市| 卓尼县| 郑州市| 东光县| 灌阳县| 东方市| 蛟河市| 桃园县| 宿州市| 本溪市| 普兰县| 和政县| 钟祥市| 宁国市| 绍兴市| 辰溪县| 墨玉县| 新疆| 和林格尔县| 元江| 错那县|