用jspsmartupload下载文件

一般无论网站还是其他系统都会用到文件的上传和下载,对于文件的上传前面已经介绍过了,在本文中将详细介绍一下文件的下载实现以及经常碰到的问题,本文代码已经过调试,可以正常使用:

文件的下载一般采用的有两种方式:通过流或利用jspsmartupload.jar下载。

一.采用数据流下载:

第一个页面:(存在“下载”按钮的页面)

  

function doDownload(filePath,disName) {

      var utl="download.jsp?filePath="+filePath+"&disName="+disName;

      document.all.opForm.action=url;

//    document.all.opForm.target="_blank";

      document.all.opForm.submit();

}

<form name="opForm" method="post" action="">

<table width="10%" name="table1">

   <tr>

      <td>

<input name=”cmdDownload”οnclick=”doDownload(‘<%=filePath%>’,’<%=fileName%>’)” value=”下载”>

      </td>

   </tr>

</table>

</form>

 第二个页面(download.jsp):

<%@ page import="java.io.OutputStream" %>

<%@ page import="java.io.FileInputStream" %>

<%

    //response.reset();

    //filePath为全路径:D:/upload/a.doc

    String path = request.getParameter("filePath");

    int k = path.lastIndexOf("/");

    String name = path.substring(k + 1 , path.length());

    response.setContentType("unknown");

    response.addHeader("Content-Disposition","filename=\"" + name + "\"");

    try {

        OutputStream os = response.getOutputStream();

        FileInputStream fis = new FileInputStream(path);

        byte[] b = new byte[1024];

        int i = 0;

        while((i = fis.read(b)) > 0) {

            os.write(b, 0 ,i);

        }

        fis.close();

        os.flush();

        os.close();

    } catch(Exception e) {

        e.printStackTrace();

    }

%>

如果只是这样写在tomcat底下可以正常使用,但weblogic底下会报如下错误:

java.net.ProtocolException: Exceeded stated content-length of: '19456' bytes

        at weblogic.servlet.internal.ServletOutputStreamImpl.checkCL(ServletOutputStreamImpl.java:219)

        at weblogic.servlet.internal.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:170)

        at com.jspsmart.upload.SmartUpload.downloadFile(SmartUpload.java:579)

        at com.jspsmart.upload.SmartUpload.downloadFile(SmartUpload.java:508)

        at jsp_servlet._page.__download._jspService(__download.java:184)

        at weblogic.servlet.jsp.JspBase.service(JspBase.java:33)

        at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1053)

        at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:387)

        at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:431)

        at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:28)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:27)

        at com.nstc.web.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:30)

        at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:27)

        at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6297)

        at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)

        at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:97)

        at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3575)

        at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2573)

        at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:178)

        at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:151)

>

这是因为weblogic会向response中写东西造成的,解决方式是将此句response.reset();的注释打开,这样在使用response时先将其重置。

二 .利用jspsmartupload.jar实现下载

  第一个页面同上

  download.jsp实现如下:

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="com.jspsmart.upload.*" %>
<%!
   public 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();
   }
%>
<%
 response.reset();//如果在weblogic底下同样要加上此句
 String filePath = request.getParameter("filePath");
 String disName = request.getParameter("disName");
  // 新建一个SmartUpload对象
 SmartUpload su = new SmartUpload();
  // 初始化
 su.initialize(pageContext);
  // 设定contentDisposition为null以禁止浏览器自动打开文件,
  //保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
  //doc时,浏览器将自动用word打开它。扩展名为pdf时,
  //浏览器将用acrobat打开。
 su.setContentDisposition(null);
  // 下载文件
    String fileName = toUtf8String(disName);

 try{
  su.downloadFile(filePath,null,fileName);
    }
    catch(java.io.FileNotFoundException e){
 %>
<script Language="javascript">
 alert('服务器上未找到要下载的文件!');
</script>
<%}
%>下载完毕!

至此文件下载介绍完毕。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值