您现在的位置:计算机技术学习网 > 技术中心 > WEB编程 > JSP >

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

来源:未知 责任编辑:智问网络 发表时间:2013-08-28 11:56 点击:
四、文件下载篇

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的形式。
    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • JSP与Servlet
    • 自己动手写MiniBBS系列(基本篇)之用户登录
    • JSP取当前日期
    • JDBC 入门(一)
    • 打开一个jsp页面默认查询所有数据,调用action
    • 使用JSP标签库验证用户的输入(2)完
    • WIN98/2000下的jsp服务器
    • 自定义JSP标签(tag)浅议
    • Struts学习傻瓜式入门篇
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1