网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 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);
    // 设定contentdisposition为null以禁止浏览器自动打开文件,
    //保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
    //doc时,浏览器将自动用word打开它。扩展名为pdf时,
    //浏览器将用acrobat打开。
    su.setcontentdisposition(null);
    // 下载文件
    su.downloadfile("https://upload.ynpxrz.com/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服务器不支持中文链接。
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/14 19:53:37