hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrei Loboda <andrei.lob...@kenception.com>
Subject The request was rejected because no multipart boundary was found
Date Wed, 08 Feb 2006 17:12:39 GMT
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?

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

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message