0

我寫了一些代碼,如果JAWS應用程序失敗,通過http發送日誌文件到服務器。爲了測試這個,我設置了一個接收和處理請求的servlet。如何使用Apache FileUppload和HttpClient?

我的客戶代碼是從Apache的HttpClient 4.2 examples

{ 
     HttpClient httpclient = new DefaultHttpClient(); 
     try { 
      HttpPost httppost = new HttpPost("http://localhost:8080/testServer/testserv/a"); 

      FileBody bin = new FileBody(new File("XXXs")); 
      StringBody comment = new StringBody("A binary file of some kind"); 

      MultipartEntity reqEntity = new MultipartEntity(); 
      reqEntity.addPart("bin", bin); 
      reqEntity.addPart("comment", comment); 

      httppost.setEntity(reqEntity); 

      System.out.println("executing request " + httppost.getRequestLine()); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity resEntity = response.getEntity(); 

      System.out.println("----------------------------------------"); 
      System.out.println(response.getStatusLine()); 
      if (resEntity != null) { 
       System.out.println("Response content length: " + resEntity.getContentLength()); 
      } 
      EntityUtils.consume(resEntity); 
     } finally { 
      try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {} 
     } 
    } 
    { 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 

     HttpPost httpPost = new HttpPost("http://localhost:8080/testServer/testserv/a"); 
     List <NameValuePair> nvps = new ArrayList <NameValuePair>(); 
     nvps.add(new BasicNameValuePair("username", "vip")); 
     nvps.add(new BasicNameValuePair("password", "secret")); 
     httpPost.setEntity(new UrlEncodedFormEntity(nvps)); 
     HttpResponse response2 = httpclient.execute(httpPost); 

     try { 
      System.out.println(response2.getStatusLine()); 
      HttpEntity entity2 = response2.getEntity(); 
      // do something useful with the response body 
      // and ensure it is fully consumed 
      EntityUtils.consume(entity2); 
     } finally { 
      httpPost.releaseConnection(); 
     } 
    } 

我的服務器端使用教程Apache FileUpload

boolean isMultipart = ServletFileUpload.isMultipartContent(request); 
    if(isMultipart){ 
     // Create a factory for disk-based file items 
     FileItemFactory factory = new DiskFileItemFactory(); 

     // Create a new file upload handler 
     ServletFileUpload upload = new ServletFileUpload(factory); 

     // Parse the request 
     try { 
      List<FileItem> items = upload.parseRequest(request); 
      Iterator iter = items.iterator(); 

      while (iter.hasNext()) { 
       FileItem item = (FileItem) iter.next(); 



       File uploadedFile = new File("XXX"); 
       try { 
        item.write(uploadedFile); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       System.out.println(uploadedFile.getAbsolutePath()); 
      } 
     } catch (FileUploadException e) { 
      System.out.println("File Upload Exception"); 
      e.printStackTrace(); 
     } 

    } else{ 
     System.out.println("Not multi part"); 
     System.out.println(request.getParameterMap()); 
    } 

我使用JBoss AS7。結果是,如果我不將FileEntity添加到MultiPartEntity,則會處理該請求。其他方面則引發FileUpload異常。

Processing of multipart/form-data request failed. Stream ended unexpectedly 

我不是很有經驗的在這裏使用JBoss和其他工具,所以你的幫助和耐心是非常感謝。

回答

1

從此question我瞭解到您需要將apache-commons-io作爲依賴項。

相關問題