java测试代码如下:
public String uploadMedia2(String cropId, String url, File file)
throws Exception {
String mediaId = "";
HttpPost httpPost = new HttpPost(url);
Closeable HttpResponse response = null;
Closeable HttpClient httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(2000).setConnectTimeout(2000).build();
httpPost.setConfig(requestConfig);
HttpEntity requestEntity = MultipartEntityBuilder
.create()
.addPart(
"media",
new FileBody(file,
ContentType.APPLICATION_OCTET_STREAM, file
.getName())).build();
httpPost.setEntity(requestEntity);
try {
response = httpClient.execute(httpPost, new BasicHttpContext());
if (response.getStatusLine().getStatusCode() != 200) {
return null;
}
HttpEntity entity = response.getEntity();
if (entity != null) {
String resultStr = EntityUtils.toString(entity, "utf-8");
JsonMapper jsonMapper = new JsonMapper();
Map<String, String> result = jsonMapper.fromJson(resultStr,
Map.class);
if (result.get("errcode") == "0") {
// 成功
result.remove("errcode");
result.remove("errmsg");
mediaId = result.get("media_id");
return mediaId;
} else {
String errMsg = (String) result.get("errmsg");
throw new Exception(errMsg);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (response != null)
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
-------------------------
public static JSONObject uploadMedia(String url, File file) {
Closeabl的Response response = null;
Closeabl的Client httpClient = getHttpClient();
if (httpClient == null) {
return null;
}
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
httpPost.setConfig(requestConfig);
HttpEntity requestEntity = MultipartEntityBuilder.create().addPart("media",
new FileBody(
file,
ContentType.APPLICATION_OCTET_STREAM,
file.getName())).build();
httpPost.setEntity(requestEntity);
try {
response = httpClient.execute(httpPost, new BasicHttpContext());
if (response.getStatusLine().getStatusCode() != 200) {
System.out.println("request url failed, http code=" + response.getStatusLine().getStatusCode()
+ ", url=" + url);
return null;
}
HttpEntity entity = response.getEntity();
if (entity != null) {
String resultStr = EntityUtils.toString(entity, "utf-8");
JSONObject result = JSON.parseObject(resultStr);
if (result.getInteger("errcode") == 0) {
return result;
} else {
System.out.println("request url=" + url + ",return value=");
System.out.println(resultStr);
}
}
} catch (Exception e) {
System.out.println("request url=" + url + ", exception, msg=" + e.getMessage());
e.printStackTrace();
} finally {
if (response != null) try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
-------------------------
引用第4楼蘇于2016-10-21 16:04发表的 回 2楼imoonstal的帖子 :
谢谢,已经解决了,还有个问题,请问知道吗?
下载文件时,不管上传上去的事什么类型的,下载下来都是XXXX.file文件,知道在哪获取到文件类型转换吗? [url=https://bbs.aliyun.com/job.php?action=topost&tid=297317&pid=839937][/url]
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。