当前位置: 移动技术网 > IT编程>开发语言>Jsp > jspSmartUpload上传下载全攻略 (四、文件下载篇 )

jspSmartUpload上传下载全攻略 (四、文件下载篇 )

2019年04月19日  | 移动技术网IT编程  | 我要评论
四、文件下载篇

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(sun企业级应用的首选)/do_download.jsp(sun企业级应用的首选)">点击下载</a></body></html>


2、下载处理页面do_download.jsp(sun企业级应用的首选) do_download.jsp(sun企业级应用的首选)展示了如何利用jsp(sun企业级应用的首选)smartupload来下载文件,从下面的源码中就可以看到,下载何其简单。

源码如下:

<%@ page contenttype="text/html;charset=gb2312" import="com.jsp(sun企业级应用的首选)smart.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、如何下载中文文件

jsp(sun企业级应用的首选)smartupload虽然能下载文件,但对中文支持不足。若下载的文件名中有汉字,则浏览器在提示另存的文件名时,显示的是一堆乱码,很扫人兴。上面的例子就是这样。(这个问题也是众多下载组件所存在的问题,很少有人解决,搜索不到相关资料,可叹!)

为了给jsp(sun企业级应用的首选)smartupload组件增加下载中文文件的支持,我对该组件进行了研究,发现对返回给浏览器的另存文件名进行utf-8编码后,浏览器便能正确显示中文名字了。这是一个令人高兴的发现。于是我对jsp(sun企业级应用的首选)smartupload组件的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");elseif(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)));elseif(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();    }


注意源码中粗体部分,原jsp(sun企业级应用的首选)smartupload组件对返回的文件未作任何处理,现在做了编码的转换工作,将文件名转换为utf-8形式的编码形式。utf-8编码对英文未作任何处理,对中文则需要转换为%xx的形式。toutf8string方法中,直接利用java语言提供的编码转换方法获得汉字字符的utf-8编码,之后将其转换为%xx的形式。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网