我在我的jsf頁面中有一個文件上傳小程序。這個applet期望一個地址可以發送它的POST請求。 (我不能編輯這個帖子請求來添加更多的字段或者其他東西)。我的servlet的post方法然後存儲文件。此作業不能由託管bean完成,因爲必須使用@MultiPartConfig對servlet進行註釋,並且無法將此註釋添加到jsf託管bean。爲了強制上傳小程序使用同一會話,我根據此topic向發佈請求添加了一個名爲jsessionId的URL屬性。現在,每當我嘗試在會話期間上傳多個文件時,applet都會在響應正文中找不到錯誤消息「The regexp string」^ SUCCESS $「,這是因爲applet希望在servlet響應中使用此字符串爲了知道上傳是成功的。Java Applet似乎在給定jsession ID時使用舊的響應對象
當我查看小程序的調試代碼響應包含了大量的HTML代碼,但沒有這樣的成功串altough我加在我的servlet代碼。這裏是我的servlet代碼:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.reset();
if (!userBean.getUploadedDatasets().isEmpty()) {
String datasetID = userBean.getUploadedDatasets().getLast().replaceAll("/", "%2F");
response.sendRedirect(response.encodeRedirectURL("http://____________/faces/details.xhtml?id="+datasetID));
}
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response){
PrintWriter out = null;
try {
Part filePart = request.getPart("item");
InputStream filecontent = filePart.getInputStream();
String datasetID = repBean.persistDataset(filecontent, uploadBean.getFolder());
userBean.getUploadedDatasets().add(datasetID);
out = response.getWriter();
out.println("SUCCESS");
} catch (ServletException ex) {
Logger.getLogger(RequestHandler.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(RequestHandler.class.getName()).log(Level.SEVERE, null, ex);
}finally{
out.close();
}
}
doGet方法是由小應用程序(所以這是給它一個文件被上傳後跳下URL)
applet代碼是作爲afterUploadURL:
<APPLET
CODE="wjhk.jupload2.JUploadApplet"
NAME="JUpload"
ARCHIVE="wjhk.jupload.jar"
WIDTH="640"
HEIGHT="300"
MAYSCRIPT="true"
ALT="The java pugin must be installed.">
<param name="postURL" value="http://________________/request;jsessionid=#{session.id}" />
<!-- Optionnal, see code comments -->
<param name="showLogWindow" value="false" />
<param name="debugLevel" value="99" />
<param name="httpUploadParameterName" value="item" />
<param name="nbFilesPerRequest" value="1" />
<param name="httpUploadParameterType" value="oneFile" />
<param name="afterUploadURL" value="http://________________/request" />
</APPLET>
,並試圖上傳第二個文件,「老字號」後(這是我的看法,但我不知道有足夠的瞭解servlet的)響應如下所示:http://pastebin.ca/2300999 (太多字符,所以我不得不把它上傳那裏)
完美地使用空的doHead方法非常感謝你! – nico1510
不客氣。 – BalusC