上传没有表格的文件 - Spring REST API
Upload file without a form - Spring REST API
我有一个 java 客户端获取 InputStream 并需要使用 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 {
但我得到以下信息:
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method
我没有任何用于浏览和选择文件的表格。在这种情况下,如何模拟 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 {
我有一个 java 客户端获取 InputStream 并需要使用 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 {
但我得到以下信息:
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method
我没有任何用于浏览和选择文件的表格。在这种情况下,如何模拟 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 {