spring - 上传没有表单的文件 - Spring REST API
问题描述
我有一个获取 InputStream 的 java 客户端,需要使用 REST API 资源从该 InputStream 上传文件。
我尝试了以下方法:
FileBody uploadFilePart = new FileBody(new File(filePath)); //filePath was extracted from the INPUTStream
// MultipartEntity reqEntity = new MultipartEntity();
// reqEntity.addPart("file", uploadFilePart);
Invocation.Builder builder = baseTarget.path(apiPath + "importProject")
.queryParam("file", uploadFilePart)
.queryParam("name", name)
.queryParam("filePath", filePath)
.queryParam("overwrite", overwrite)
.request(/*MediaType.APPLICATION_JSON*/);
Response response = builder.post(Entity.json(null));
或者:
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
//builder.addTextBody("field1", "yes", ContentType.TEXT_PLAIN);
// This attaches the file to the POST:
File f = new File(filePath);
entityBuilder.addBinaryBody(
"file",
new FileInputStream(f),
ContentType.APPLICATION_OCTET_STREAM,
f.getName()
);
HttpEntity multipart = entityBuilder.build();
//uploadFile.setEntity(multipart);
Invocation.Builder builder = baseTarget.path(apiPath + "importProject")
.queryParam("file", multipart)
.queryParam("name", name)
.queryParam("filePath", filePath)
.queryParam("overwrite", overwrite)
.request(/*MediaType.APPLICATION_JSON*/);
Response response = builder.post(Entity.json(null));
或者:
final FileDataBodyPart filePart = new FileDataBodyPart("file", new File(filePath));
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
final FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart.field("name", "value").bodyPart(filePart);
Invocation.Builder builder = baseTarget.path(apiPath + "importProject")
.queryParam("file", multipart)
.queryParam("name", name)
.queryParam("filePath", filePath)
.queryParam("overwrite", overwrite)
.request(MediaType.MULTIPART_FORM_DATA);
Response response = builder.post(/*Entity.entity(multipart, multipart.getMediaType())*/ Entity.json(null));
formDataMultiPart.close();
multipart.close();
在控制器中:
@RequestMapping(method=RequestMethod.POST, value="importProject", headers = "content-type=multipart/*")
public ResponseEntity<Void> importProject(@RequestParam("file") MultipartFile file,
@RequestParam(value="name", required = true) String name,
@RequestParam(value="filePath", required = true) String filePath,
@RequestParam(value="overwrite", required = false) boolean overwrite,
@RequestParam(value="userSessionId", required = false) String userSessionId)
throws ProjectManagementException, IOException {
但我得到以下信息:
服务器拒绝此请求,因为请求实体的格式不受所请求方法的请求资源支持
我没有任何用于浏览和选择文件的表格。在这种情况下,如何模拟 FormData 并将其发送到 rest?
解决方案
我花了一段时间才找到这个 Jersy Java 代码示例: https ://www.programcreek.com/java-api-examples/?api=org.glassfish.jersey.media.multipart.MultiPart
客户:
final FileDataBodyPart filePart = new FileDataBodyPart("file", new File(filePath));
MultiPart multipart = new FormDataMultiPart()
.field("foo", "bar")
.bodyPart(filePart);
Invocation.Builder builder = baseTarget.path(apiPath + "importProject")
.register(MultiPartFeature.class)
//.queryParam("file", multipart)
.queryParam("name", name)
.queryParam("filePath", filePath)
.queryParam("overwrite", overwrite)
.request(MediaType.MULTIPART_FORM_DATA);
Response response = builder.post(Entity.entity(multipart, multipart.getMediaType()) ); //new MediaType("application", "utf-8")
休息:
@RequestMapping(method=RequestMethod.POST, value="importProject", headers = "content-type=multipart/*")
public ResponseEntity<Void> importProject(/*@RequestParam("file")*/ @RequestBody MultipartFile file,
@RequestParam(value="name", required = true) String name,
@RequestParam(value="filePath", required = true) String filePath,
@RequestParam(value="overwrite", required = false) boolean overwrite,
@RequestParam(value="userSessionId", required = false) String userSessionId)
throws ProjectManagementException, IOException {
推荐阅读
- google-chrome - Google Lighthouse 如何将我的网站明确标记为不是 PWA
- node.js - 在 Gulp 中运行异步任务的问题
- html - 如何使用 HTML 和 CSS 正确格式化和分割图像以位于文本的右侧
- pascal - 在 Pascal 中创建一个类
- typescript - 通过 webpack 将 yaml 导入打字稿
- javascript - JS - 如何更改箭头键在文本中移动光标的方式
- asp.net-mvc - 如何在MVC中使用if条件更改html标签数据
- node.js - 如何在 nodejs 中获取动态 url?(人栈)
- javascript - 如何将一个数字尽可能均匀地分配到一个数组中?
- frameworks - 框架不可知论是什么意思?