Java:将Multipart文件发送到RESTWebservice
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java:将Multipart文件发送到RESTWebservice,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2536字,纯文字阅读大概需要4分钟。
内容图文
我使用Spring作为技术.用于调用Webservice的Apache HttpClient.基本目的是上传多部分文件(文件不是任何图像文件或视频文件).但在这里我的要求略有不同.请检查下面的图像.
在这里你可以看到第一块.它是一个简单的GUI,只有2个输入标签和正确的表格标签.
在第二个块中,有RESTFul Webservice,它将Multipart File作为参数并进行处理. (到此为止).
现在我被困在这里.我想将此Multipart文件发送到仅使用Multipart File的其他RESTFul Web服务.
RESTFUL Webservice的代码片段:(评论了一些问题,需要你的建议)
@RequestMapping(value="/project/add")
public @ResponseBody String addURLListToQueue(
@RequestParam(value = "file") MultipartFile file,
@RequestParam(value = "id1", defaultValue = "notoken") String projectName,
@RequestParam(value = "id2", defaultValue = "notoken") String id2,
@RequestParam(value = "id3", defaultValue = "notoken") String id3){
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost("http://localhost:8080/fs/project/add");
// 1) Do I need to convert it into File object? (tried but faced 400 Error)
// 2) Is there any provision to send Multipart File as it is?
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "multipart/form-data");
mpEntity.addPart("file", cbFile);
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
}
}
问题:如何使用HttpClient将Multipart文件发送到RESTFul Webservice?
解决方法:
你能看看file upload apache http client吗?
也
HttpPost post = new HttpPost("http://echo.200please.com");
InputStream inputStream = new FileInputStream(zipFileName);
File file = new File(imageFileName);
String message = "This is a multipart post";
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody
("upfile", file, ContentType.DEFAULT_BINARY, imageFileName);
builder.addBinaryBody
("upstream", inputStream, ContentType.create("application/zip"), zipFileName);
builder.addTextBody("text", message, ContentType.TEXT_PLAIN);
//
HttpEntity entity = builder.build();
post.setEntity(entity);
HttpResponse response = client.execute(post);
内容总结
以上是互联网集市为您收集整理的Java:将Multipart文件发送到RESTWebservice全部内容,希望文章能够帮你解决Java:将Multipart文件发送到RESTWebservice所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。