首页 > 开发 > JSP > 正文

使用jspSmartUpload的下载文件的经验教训

2020-02-05 13:37:29
字体:
来源:转载
供稿:网友
国内最大的酷站演示中心!

  昨天晚上用jspsmartupload下载文件,写了一个下载的页面(download.jsp)如下:

<%@ page contenttype="text/html;charset=gb2312" %>
 <html>
<%@ page import="java.io.*,mail.util.encoding" %>
<jsp:usebean id="mysmartupload" scope="page" class="com.jspsmart.upload.smartupload" />
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<%
  string filename =encoding.encodingunicode(request.getparameter("filename"));
  mail.dbbase dbbase=mail.dbbase.getinstance();
  string path=dbbase.getpath();
  // 初始化
  mysmartupload.initialize(pagecontext);
  // 设定contentdisposition为null以禁止浏览器自动打开文件,
  //保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
  //doc时,浏览器将自动用word打开它。扩展名为pdf时,
  //浏览器将用acrobat打开。
  mysmartupload.setcontentdisposition(null);
  // 下载文件
  if(filename!=null){
    mysmartupload.downloadfile(path+filename);
  }
%>
</body>
</html>

  执行下载时,文件能正确下载,但是总是报错。

java.lang.illegalstateexception: getoutputstream() has already been called for this response

  后来还才发现,执行下载的页面,在java脚本范围外(即<% ...%>之外),不要包含html代码、空格、回车或换行等字符,有的话将不能正确下载。不信的话,可以在上述源码中%><%之间加入一个换行符,再下载一下,保证出错。因为它影响了返回给浏览器的数据流,导致解析出错。

  修改后问题解决。

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