Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

java - The type MultipartEntity is deprecated

The documentation says the org.apache.http.entity.mime.MultipartEntity class is deprecated. Could anybody please suggest me an alternative ?

I am using this in my code like this:

entity.addPart("params", new StringBody("{"auth":{"key":""
            + authKey + ""},"template_id":"" + templateId + ""}"));
entity.addPart("my_file", new FileBody(image));
httppost.setEntity(entity);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you read the docs carefully, you'll notice that you should use MultipartEntityBuilder as an alternative.

For example:

MultipartEntityBuilder builder = MultipartEntityBuilder.create();        

/* example for setting a HttpMultipartMode */
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

/* example for adding an image part */
FileBody fileBody = new FileBody(new File(image)); //image should be a String
builder.addPart("my_file", fileBody); 
//and so on

Note that there are several constructors for the FileBody class, by which you can provide mimeType, content type, etc.

After you're done with passing build instructions to the builder, you can get the built HttpEntity by invoking the MultipartEntityBuilder#build() method:

HttpEntity entity = builder.build();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...