文档说org.apache.http.entity.mime.MultipartEntity http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html类已被弃用。有人可以建议我一个替代方案吗?
我在我的代码中使用它,如下所示:
entity.addPart("params", new StringBody("{\"auth\":{\"key\":\""
+ authKey + "\"},\"template_id\":\"" + templateId + "\"}"));
entity.addPart("my_file", new FileBody(image));
httppost.setEntity(entity);
如果您仔细阅读文档,您会发现您应该使用MultipartEntityBuilder http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntityBuilder.html作为备选。
例如:
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
请注意,有几个构造函数FileBody http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/FileBody.html类,您可以通过它提供mimeType, 内容类型, etc.
当你完成通过后构建说明给建造者,你可以得到建造的HttpEntity http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpEntity.html?is-external=true通过调用MultipartEntityBuilder#build() http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntityBuilder.html method:
HttpEntity entity = builder.build();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)