jsp 文件下載代碼分享

字號:


    jsp教程 文件下載代碼分享
    1、下載鏈接頁面download.html
    頁面源碼如下:
    代碼如下:
    <!--
    文件名:download.html
    作 者:縱橫軟件制作中心雨亦奇(zhsoft88@sohu.com)
    -->
    <!doctype html public "-//w3c//dtd html 4.01 transitional//en">
    <html>
    <head>
    <title>下載</title>
    <meta http-equiv="content-type" content="text/html; charset=gb2312">
    </head>
    <body>
    <a href="jsp/do_download.jsp">點擊下載</a>
    </body>
    </html>
    2、下載處理頁面do_download.jsp do_download.jsp展示了如何利用jsp教程martupload組件來下載文件,從下面的源碼中就可以看到,下載何其簡單。
    源碼如下:
    <%@ page contenttype="text/html;charset=gb2312"
    import="com.jspsmart.upload.*" %><%
    // 新建一個smartupload對象
    smartupload su = new smartupload();
    // 初始化
    su.initialize(pagecontext);
    // 設(shè)定contentdisposition為null以禁止瀏覽器自動打開文件,
    //保證點擊鏈接后是下載文件。若不設(shè)定,則下載的文件擴展名為
    //doc時,瀏覽器將自動用word打開它。擴展名為pdf時,
    //瀏覽器將用acrobat打開。
    su.setcontentdisposition(null);
    // 下載文件
    su.downloadfile("https://upload.ynpxrz.com/upload/如何賺取我的第一桶金.doc");
    %>
    注意,執(zhí)行下載的頁面,在java腳本范圍外(即<% ... %>之外),不要包含html代碼、空格、回車或換行等字符,有的話將不能正確下載。不信的話,可以在上述源碼中%><%之間加入一個 換行符,再下載一下,保證出錯。因為它影響了返回給瀏覽器的數(shù)據(jù)流,導(dǎo)致解析出錯。
    3、如何下載中文文件
    jspsmartupload雖然能下載文件,但對中文支持不足。若下載的文件名中有漢字,則瀏覽器在提示另存的文件名時,顯示的是一堆亂碼,很掃人興。上面的例子就是這樣。(這個問題也是眾多下載組件所存在的問題,很少有人解決,搜索不到相關(guān)資料,可嘆?。?BR>    為了給jspsmartupload組件增加下載中文文件的支持,我對該組件進行了研究,發(fā)現(xiàn)對返回給瀏覽器的另存文件名進行utf-8編碼后,瀏 覽器便能正確顯示中文名字了。這是一個令人高興的發(fā)現(xiàn)。于是我對jspsmartupload組件的smartupload類做了升級處理,增加了 toutf8string這個方法,改動部分源碼如下:
    public void downloadfile(string s, string s1, string s2, int i)
    throws servletexception, ioexception, smartuploadexception
    {
    if(s == null)
    throw new illegalargumentexception("file ''" + s +
    "'' not found (1040).");
    if(s.equals(""))
    throw new illegalargumentexception("file ''" + s +
    "'' not found (1040).");
    if(!isvirtual(s) && m_denyphysicalpath)
    throw new securityexception("physical path is
    denied (1035).");
    if(isvirtual(s))
    s = m_application.getrealpath(s);
    java.io.file file = new java.io.file(s);
    fileinputstream fileinputstream = new fileinputstream(file);
    long l = file.length();
    boolean flag = false;
    int k = 0;
    byte abyte0[] = new byte[i];
    if(s1 == null)
    m_response.setcontenttype("application/x-msdownload");
    else
    if(s1.length() == 0)
    m_response.setcontenttype("application/x-msdownload");
    else
    m_response.setcontenttype(s1);
    m_response.setcontentlength((int)l);
    m_contentdisposition = m_contentdisposition != null ?
    m_contentdisposition : "attachment;";
    if(s2 == null)
    m_response.setheader("content-disposition",
    m_contentdisposition + " filename=" +
    toutf8string(getfilename(s)));
    else
    if(s2.length() == 0)
    m_response.setheader("content-disposition",
    m_contentdisposition);
    else
    m_response.setheader("content-disposition",
    m_contentdisposition + " filename=" + toutf8string(s2));
    while((long)k < l)
    {
    int j = fileinputstream.read(abyte0, 0, i);
    k += j;
    m_response.getoutputstream().write(abyte0, 0, j);
    }
    fileinputstream.close();
    }
    /**
    * 將文件名中的漢字轉(zhuǎn)為utf8編碼的串,以便下載時能正確顯示另存的文件名.
    * 縱橫軟件制作中心雨亦奇2003.08.01
    * @param s 原文件名
    * @return 重新編碼后的文件名
    */
    public static string toutf8string(string s) {
    stringbuffer sb = new stringbuffer();
    for (int i=0;i<s.length();i++) {
    char c = s.charat(i);
    if (c >= 0 && c <= 255) {
    sb.append(c);
    } else {
    byte[] b;
    try {
    b = character.tostring(c).getbytes("utf-8");
    } catch (exception ex) {
    system.out.println(ex);
    b = new byte[0];
    }
    for (int j = 0; j < b.length; j++) {
    int k = b[j];
    if (k < 0) k += 256;
    sb.append("%" + integer.tohexstring(k).
    touppercase());
    }
    }
    }
    return sb.tostring();
    }
    注意源碼中粗體部分,原jspsmartupload組件對返回的文件未作任何處理,現(xiàn)在做了編碼的轉(zhuǎn)換工作,將文件名轉(zhuǎn)換為utf-8形式的編碼 形式。utf-8編碼對英文未作任何處理,對中文則需要轉(zhuǎn)換為%xx的形式。toutf8string方法中,直接利用java語言提供的編碼轉(zhuǎn)換方法獲 得漢字字符的utf-8編碼,之后將其轉(zhuǎn)換為%xx的形式。
    將源碼編譯后打包成jspsmartupload.jar,拷貝到tomcat的shared/lib目錄下(可為所有web應(yīng)用程序所共享),然 后重啟tomcat服務(wù)器就可以正常下載含有中文名字的文件了。另,toutf8string方法也可用于轉(zhuǎn)換含有中文的超級鏈接,以保證鏈接的有效,因 為有的web服務(wù)器不支持中文鏈接。