首页 > 解决方案 > 上传没有表单的文件 - 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?

标签: springrestmultipartform-data

解决方案


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

推荐阅读