首页 > 开发 > JSP > 正文

jspSmartUpload上传下载全攻略(四)

2020-02-05 13:57:28
字体:
来源:转载
供稿:网友

四、文件下载篇

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展示了如何利用jspsmartupload组件来下载文件,从下面的源码中就可以看到,下载何其简单。

源码如下:

<%@ 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服务器不支持中文链接。

小结:jspsmartupload组件是应用jsp进行b/s程序开发过程中经常使用的上传下载组件,它使用简单,方便。现在我又为其加上了下载中文名字的文件的支持,真个是如虎添翼,必将赢得更多开发者的青睐。

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表