2013-01-10 21 views
1

我在我的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 (太多字符,所以我不得不把它上傳那裏)

回答

2

您的問題是由於servlet正在發送重定向GET(以及隱含HEAD)請求而引起的。日誌下面的部分包含相關信息:

00064 13:44:42.482  HttpProtocolFinderThread  [DEBUG]  Getting serverProtocol from HEAD request 
00065 13:44:42.485  HttpProtocolFinderThread  [DEBUG]  Checking protocol with URL: http://___________________:8080/application1/request;jsessionid=47440f88e532021656b3724eea33 
00066 13:44:42.501  HttpProtocolFinderThread  [DEBUG]  [initByteArrayEncoder] proxy=DIRECT, proxy.type=DIRECT, useProxy=false, url.host=___________________, url.port=8080 
00067 13:44:42.501  HttpProtocolFinderThread  [DEBUG]  [onAppendHeader] Start 
00068 13:44:42.502  HttpProtocolFinderThread  [DEBUG]  [onAppendHeader] Header appended; Cookie: __utma=132868616.170774110.1354796930.1356109339.1356547898.3; __utmz=132868616.1356547898.3.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided) 
00069 13:44:42.509  HttpProtocolFinderThread  [DEBUG]  [onAppendHeader] Header appended; User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:16.0) Gecko/20100101 Firefox/16.0 
00070 13:44:42.510  HttpProtocolFinderThread  [DEBUG]  [onAppendHeader] End 
00071 13:44:42.510  HttpProtocolFinderThread  [DEBUG]  [HTTPConnectionHelper append] 
00072 13:44:42.511  HttpProtocolFinderThread  [DEBUG]  Before sendRequest() 
00073 13:44:42.511  HttpProtocolFinderThread  [DEBUG]  Using non SSL socket, direct connection 
00074 13:44:42.538  HttpProtocolFinderThread  [DEBUG]  After sendRequest() 
00075 13:44:42.538  HttpProtocolFinderThread  [DEBUG]  ------------------------------------------------------------------------- 
00076 13:44:42.538  HttpProtocolFinderThread  [DEBUG]  ----------------- HEAD message sent (start) -------------------------- 
00077 13:44:42.539  HttpProtocolFinderThread  [DEBUG]  ------------------------------------------------------------------------- 
00078 13:44:42.539  HttpProtocolFinderThread  [DEBUG]  HEAD /application1/request;jsessionid=47440f88e532021656b3724eea33 HTTP/1.1 
00078 13:44:42.539  HttpProtocolFinderThread  [DEBUG]  Host: ___________________:8080 
00078 13:44:42.539  HttpProtocolFinderThread  [DEBUG]  Accept: */* 
00078 13:44:42.539  HttpProtocolFinderThread  [DEBUG]  Accept-Encoding: identity 
00078 13:44:42.539  HttpProtocolFinderThread  [DEBUG]  Connection: close 
00078 13:44:42.539  HttpProtocolFinderThread  [DEBUG]  Cookie: __utma=132868616.170774110.1354796930.1356109339.1356547898.3; __utmz=132868616.1356547898.3.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided) 
00078 13:44:42.539  HttpProtocolFinderThread  [DEBUG]  User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:16.0) Gecko/20100101 Firefox/16.0 
00078 13:44:42.539  HttpProtocolFinderThread  [DEBUG]  
00079 13:44:42.539  HttpProtocolFinderThread  [DEBUG]  ------------------------------------------------------------------------- 
00080 13:44:42.540  HttpProtocolFinderThread  [DEBUG]  ----------------- HEAD message sent (end) ----------------------------- 
00081 13:44:42.540  HttpProtocolFinderThread  [DEBUG]  ------------------------------------------------------------------------- 
00082 13:44:42.548  HttpProtocolFinderThread  [DEBUG]  -------- Response Headers Start -------- 
00083 13:44:42.556  HttpProtocolFinderThread  [DEBUG]  HTTP/1.1 302 Moved Temporarily 
00084 13:44:42.558  HttpProtocolFinderThread  [DEBUG]  Location: http://___________________:8080/application1/faces/details.xhtml;jsessionid=47440f88e532021656b3724eea33?id=%2F37ded64f-f2c9-4b90-99ea-79c34eb140e5 
00085 13:44:42.559  HttpProtocolFinderThread  [DEBUG]  Content-Type: text/html;charset=ISO-8859-1 
00086 13:44:42.566  HttpProtocolFinderThread  [DEBUG]  Content-Language: de-DE 
00087 13:44:42.578  HttpProtocolFinderThread  [DEBUG]  Content-Length: 306 
00088 13:44:42.578  HttpProtocolFinderThread  [DEBUG]  Date: Thu, 10 Jan 2013 12:44:42 GMT 
00089 13:44:42.579  HttpProtocolFinderThread  [DEBUG]  Connection: close 
00090 13:44:42.579  HttpProtocolFinderThread  [DEBUG]  
00091 13:44:42.580  HttpProtocolFinderThread  [DEBUG]  --------- Response Headers End --------- 
00092 13:44:42.580  HttpProtocolFinderThread  [DEBUG]  This is a HEAD request: we don't care about the bytearrayResponseBody 
00093 13:44:42.584  HttpProtocolFinderThread  [DEBUG]  HEAD status: 302 
00094 13:44:42.585  HttpProtocolFinderThread  [DEBUG]  HEAD protocol: HTTP/1.1 
00095 13:44:42.586  HttpProtocolFinderThread  [DEBUG]  Location read: http://___________________:8080/application1/faces/details.xhtml;jsessionid=47440f88e532021656b3724eea33?id=%2F37ded64f-f2c9-4b90-99ea-79c34eb140e5 
00096 13:44:42.594  HttpProtocolFinderThread  [INFO]  postURL switched from http://___________________:8080/application1/request;jsessionid=47440f88e532021656b3724eea33 to http://___________________:8080/application1/faces/details.xhtml;jsessionid=47440f88e532021656b3724eea33?id=%2F37ded64f-f2c9-4b90-99ea-79c34eb140e5 

小程序基本上是由HEAD請求測試的postURL可用性,一旦確定,它被重定向,它使用重定向的URL作爲新postURL(按照複製日誌的最後一行)。但是,重定向的URL指向一個返回大量HTML的JSF頁面。

你需要修復你的doGet(),它不執行重定向,或者添加一個doHead(),它無效地返回任何東西。由於HEAD根據HTTP規範(基本上是沒有響應主體的GET),默認doHead()實現委託給doGet()並放棄響應主體。覆蓋doHead()應該防止它委託給doGet()

+0

完美地使用空的doHead方法非常感謝你! – nico1510

+0

不客氣。 – BalusC