昨天晚上用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代碼、空格、回車或換行等字符,有的話將不能正確下載。不信的話,可以在上述源碼中%><%之間加入一個換行符,再下載一下,保證出錯。因為它影響了返回給瀏覽器的數據流,導致解析出錯。
修改后問題解決。
新聞熱點
疑難解答