添加依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>
import org.json.JSONObject;
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uploadUrl);
StringBody bucketName = new StringBody("xxxx", ContentType.create("text/plain", Consts.UTF_8));
HttpEntity reqEntity = MultipartEntityBuilder.create()
// 相当于<input type="file" name="file"/>
.addPart("file", new FileBody(file))
// 相当于<input type="text" name="bucketName" value=bucketName>
.addPart("bucketName", bucketName)
.build();
httpPost.setEntity(reqEntity);
HttpResponse response = client.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
result = EntityUtils.toString(responseEntity);
JSONObject jsonObject = new JSONObject(result);
String code = jsonObject.get("code").toString();
该博客介绍了如何利用Apache HttpClient库执行HTTP POST请求,将文件和文本数据上传到指定URL,并接收JSON格式的响应。通过示例代码展示了MultipartEntityBuilder创建多部分请求实体,以及如何解析返回的JSON结果获取特定字段。

1375

被折叠的 条评论
为什么被折叠?



