添加依赖
<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();
|