上载没有表单的文件-Spring REST API

时间:2019-02-18 12:35:36

标签: spring rest multipartform-data

我有一个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 {

但是我得到以下信息:

  

服务器拒绝了此请求,因为请求实体的格式不受请求的方法所请求的资源支持

我没有用于浏览和选择文件的任何形式。在这种情况下,如何模拟FormData并将其发送给其他用户?

1 个答案:

答案 0 :(得分:0)

花了我一段时间才能看到这个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 {