程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> jsp 文件下載代碼分享

jsp 文件下載代碼分享

編輯:關於JSP

jsp教程 文件下載代碼分享

1、下載鏈接頁面download.html

頁面源碼如下:

<!--
文件名:download.html
作  者:縱橫軟件制作中心雨亦奇([email protected])
-->
<!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展示了如何利用jsps教程martupload組件來下載文件,從下面的源碼中就可以看到,下載何其簡單。

源碼如下:

<%@ page contenttype="text/html;charset=gb2312"
import="com.jspsmart.upload.*" %><%
// 新建一個smartupload對象
smartupload su = new smartupload();
// 初始化
su.initialize(pagecontext);
// 設定contentdisposition為null以禁止浏覽器自動打開文件,
//保證點擊鏈接後是下載文件。若不設定,則下載的文件擴展名為
//doc時,浏覽器將自動用word打開它。擴展名為pdf時,
//浏覽器將用acrobat打開。
su.setcontentdisposition(null);
// 下載文件
su.downloadfile("/upload/如何賺取我的第一桶金.doc");
%>
 


注意,執行下載的頁面,在java腳本范圍外(即<% ... %>之外),不要包含html代碼、空格、回車或換行等字符,有的話將不能正確下載。不信的話,可以在上述源碼中%><%之間加入一個換行符,再下載一下,保證出錯。因為它影響了返回給浏覽器的數據流,導致解析出錯。

3、如何下載中文文件

jspsmartupload雖然能下載文件,但對中文支持不足。若下載的文件名中有漢字,則浏覽器在提示另存的文件名時,顯示的是一堆亂碼,很掃人興。上面的例子就是這樣。(這個問題也是眾多下載組件所存在的問題,很少有人解決,搜索不到相關資料,可歎!)

為了給jspsmartupload組件增加下載中文文件的支持,我對該組件進行了研究,發現對返回給浏覽器的另存文件名進行utf-8編碼後,浏覽器便能正確顯示中文名字了。這是一個令人高興的發現。於是我對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();
    }

    /**
     * 將文件名中的漢字轉為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組件對返回的文件未作任何處理,現在做了編碼的轉換工作,將文件名轉換為utf-8形式的編碼形式。utf-8編碼對英文未作任何處理,對中文則需要轉換為%xx的形式。toutf8string方法中,直接利用java語言提供的編碼轉換方法獲得漢字字符的utf-8編碼,之後將其轉換為%xx的形式。

將源碼編譯後打包成jspsmartupload.jar,拷貝到tomcat的shared/lib目錄下(可為所有web應用程序所共享),然後重啟tomcat服務器就可以正常下載含有中文名字的文件了。另,toutf8string方法也可用於轉換含有中文的超級鏈接,以保證鏈接的有效,因為有的web服務器不支持中文鏈接。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved