Search code examples
javaapache-httpclient-4.xmultipart

how to handle multipart request using http client


Following is my code to handle Multipart using httpclient

if(methodParams.getDataType().length()>0 && methodParams.getDataType().equals("org.springframework.web.multipart.MultipartFile")){
                isMultipart = true; 
                MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );
                // For usual String parameters
                entity.addPart( methodParams.getVariableDefined(), new StringBody("".toString() , "text/plain", Charset.forName( "UTF-8" )));
                postURL.setEntity( entity );
             }

but i get the following exception :

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.james.mime4j.util.CharsetUtil.getCharset(Ljava/lang/String;)Ljava/nio/charset/Charset;
    at org.apache.http.entity.mime.MIME.<clinit>(MIME.java:51)
    at org.apache.http.entity.mime.HttpMultipart.<clinit>(HttpMultipart.java:85)
    at org.apache.http.entity.mime.MultipartEntity.<init>(MultipartEntity.java:77)
    at org.apache.http.entity.mime.MultipartEntity.<init>(MultipartEntity.java:96)
    at com.hexgen.tools.HexgenClassUtils.doPOST(HexgenClassUtils.java:151)
    at com.hexgen.reflection.HttpClientRequests.handleHTTPRequest(HttpClientRequests.java:74)
    at com.hexgen.reflection.HexgenWebAPITest.main(HexgenWebAPITest.java:115)

EDIT:

following are the dependency i use

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.0.1</version>
</dependency>

how to solve this.


Solution

  • You can take a look at dependencies one more time, perhaps you've missed some jars.

    You may also replace your old jars with newer version of a httpclient along with httpmime. httpclient is no longer relying on james mime4j since version 4.1.

    You may also end up managing your dependencies with maven. Just in case if you are not using it.

    Edit:

    You may add the following

    <dependency>
        <groupId>org.apache.james</groupId>
        <artifactId>apache-mime4j</artifactId>
        <version>0.6</version>
    </dependency>