hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: The request was rejected because no multipart boundary was found
Date Thu, 09 Feb 2006 10:29:09 GMT
On Wed, 2006-02-08 at 12:12 -0500, Andrei Loboda wrote:
> I used HttpClient 2.0.2 to upload files to web server and it worked fine. 
> 
> Then I changed HttpClient to version 3.0 and rewritten uploading by using 
> MultipartRequestEntity like in the example. After that server side always 
> generates error: 
> "the request was rejected because no multipart boundary was found"
> 
> On web server it process by FileUpload 1.0. 
> 
> Any suggestion?

Andrei,
Create and compare wire logs of HTTP sessions generated with HttpClient
2.0.2 and HttpClient 3.0 and see in which ways they differ. It should be
pretty trivial to tweak HttpClient 3.0 to make it produce multipart
content identical to that produced by HttpClient 2.0.2 

Oleg

> 
> Client code HttpClient 2.0.2:
> 
>  public boolean uploadFile(File file) { 
>         int responceCode; 
> 
>         boolean flag = false; 
>         if (file.exists()) {
>             MultipartPostMethod filePost = new 
> MultipartPostMethod(serverURL.toString() + uploadUrlStr); 
>             try {
>                 filePost.addParameter("fileupload", file);
>                 responceCode = clientHttp.executeMethod(filePost);
>                 switch (responceCode) {
>                     case 401:
>                         isLoggedIn = false;
>                         break;
>                     case 200:
>                         flag = true;
>                         break;
>                 }
> 
>             } catch (Exception ex) {
>                 System.out.println("Error: " + ex.toString());
>                 ex.printStackTrace();
>             } finally {
>                 filePost.releaseConnection();
>             }
>         } 
>         return flag;
>     }
> 
> Client code HttpClient 3.0:
> 
> public boolean uploadFile(File file) {
>         int responceCode; 
> 
>         boolean flag = false; 
>         if (file.exists()) {
>             PostMethod filePost = new PostMethod(serverURL.toString() + 
> uploadUrlStr);
>             currentMethod = filePost;
>             try { 
>                 Part[] parts = {new FilePart("fileupload", file)};
>  
> filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, 
> true); 
>                 HttpMethodParams params = filePost.getParams();
>                 filePost.setRequestEntity(new 
> MultipartRequestEntity(parts, filePost.getParams()));
>                 responceCode = clientHttp.executeMethod(filePost);
>                 switch (responceCode) {
>                     case 401:
>                         isLoggedIn = false;
>                         break;
>                     case 200:
>                         flag = true;
>                         break;
>                 }
>                 lastServerResposeCode = responceCode;
> 
>             } catch (Exception ex) {
>                 System.out.println("Error: " + ex.toString());
>                 ex.printStackTrace();
>                 lastServerResposeCode = -1;
>             } finally {
>                 currentMethod = null;
>                 filePost.releaseConnection();
>             }
>         } else {
>             lastServerResposeCode = -1;
>         }
> 
>         return flag;
>     }
> 
> 
> 
> Server code :
> 
> public File UploadFile(HttpServletRequest uploadHttpRequest) {
>         DefaultFileItemFactory dfFileItemFactory = new 
> DefaultFileItemFactory(
>                         10240, new File(tempFolderPatchStr));
>         DiskFileUpload mainFU = new DiskFileUpload(dfFileItemFactory);
>         List fields; 
>         InputStream uploadInputStream;
>         File uploadedFile = null;
>         String fileName = null;
>         String tempDir;
>         DefaultFileItem uploadingFileItem;
> 
>         try {
>                 fields = mainFU.parseRequest(uploadHttpRequest);//this 
> line generate error: 
>                 //"the request was rejected because no multipart boundary 
> was found" 
>                 uploadingFileItem = (DefaultFileItem) getFileItem(fields);
>                 tempDir = getTempDirectory();
>                 if (tempDir != null) {
>                         fileName = uploadingFileItem.getName();
>                         uploadedFile = new File(tempDir + fileName);
>                         if (uploadingFileItem.isInMemory()) {
>                                 uploadInputStream = 
> uploadingFileItem.getInputStream();
>                                 uploadedFile.createNewFile();
>                                 FileOutputStream tempFileOS = new 
> FileOutputStream(
>                                                 uploadedFile);
>                                 BufferedInputStream 
> uploadBufferedInputStream = new BufferedInputStream(
>                                                 uploadInputStream, 1024);
>                                 byte[] buffer = new byte[1024];
>                                 int bytesRead;
>                                 while ((bytesRead = 
> uploadBufferedInputStream
>                                                 .read(buffer)) != -1)
>                                         tempFileOS.write(buffer, 0, 
> bytesRead);
>                                 tempFileOS.close();
>                         } else {
>  uploadingFileItem.getStoreLocation().renameTo(
>                                                 uploadedFile);
>                         }
>                 } else {
>                         uploadedFile = null;
>                 } 
>         } catch (Exception ex) {
>                 System.out.println(ex.getMessage());
>                 uploadedFile = null;
>         } 
> return uploadedFile;
> }
> 
> 
> Thanks,
> 
> Andrei Loboda


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Mime
View raw message