繁体   English   中英

SpringBoot WebService客户端:MultipartFile和String参数

[英]SpringBoot WebService client: MultipartFile and String parameters

我已经使用SpringBoot实现了一个上传REST Web服务,该服务接收2个参数:

  • 字串讯息
  • 一份文件

Web服务代码如下所示:

@RequestMapping(value = "/uploadtest", consumes = { MediaType.MULTIPART_FORM_DATA}, method = RequestMethod.POST)
public ResponseEntity<Map<String, String>> upload(
                                                  @RequestParam("msg") String  msg,
                                                  @RequestParam("file") MultipartFile file) {

    System.out.println("uploadtest");
    return new ResponseEntity<>(singletonMap("url", "uploadtest"), HttpStatus.CREATED);

}

我正在尝试创建Jersey WS客户端。 当WS仅接收MultipartFile参数时,以下代码可以正常工作:

Client client = ClientBuilder.newBuilder()
            .register(MultiPartFeature.class).build();
    WebTarget webTarget
            = client.target("http://localhost:8080/uploadtest");

    MultiPart multiPart = new MultiPart();
    multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);

    FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
            new File("/filename.xml"),
            MediaType.APPLICATION_OCTET_STREAM_TYPE);
    multiPart.bodyPart(fileDataBodyPart);

    Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE)
            .post(Entity.entity(multiPart, multiPart.getMediaType()));

此外,如果两个参数均为String,则以下代码也适用:

Client client = ClientBuilder.newClient(clientConfig);
    WebTarget webTarget
            = client.target("http://localhost:8080/uploadtest");

    MultivaluedMap<String, String> formData = new MultivaluedHashMap<String, String>();
    formData.add("msg", "msg1");
    formData.add("mesgbis", "msg2");

    String responseResult = webTarget.request()
            .post(Entity.entity(formData, MediaType.MULTIPART_FORM_DATA), String.class);

我想了解是否有一种方法可以在MultiPart对象上创建bodyPart以便创建String和MultipartFile参数。 如果不是这样,我该如何完成对WS的请求?

最后,我设法使其工作如下:

Client client = ClientBuilder.newBuilder()
            .register(MultiPartFeature.class).build();
    WebTarget webTarget
            = client.target("http://localhost:8080/uploadtest");

    MultiPart multiPart = new MultiPart();
    multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);

    FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
            new File("/filename.xml"),
            MediaType.APPLICATION_OCTET_STREAM_TYPE);

    FormDataBodyPart bodyPartMsg = new FormDataBodyPart("msg", "custom msg");
    multiPart.bodyPart(bodyPartMsg);
    multiPart.bodyPart(fileDataBodyPart);

    Response response = webTarget.request(MediaType.APPLICATION_JSON_TYPE)
            .post(Entity.entity(multiPart, multiPart.getMediaType()));

顺便说一句,可以通过使用OkHttp来实现相同的行为:

File uploadFile = new File(pathUploadFile);
RequestBody formBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("msg", job.toString())
            .addFormDataPart("mesgbis", uploadFile.getName(), RequestBody.create(null, uploadFile))
            .build();

Request request = new Request.Builder()
            .url(url)
            .post(formBody)
            .build();

String retValue = "";
Response response = client.newCall(request).execute();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM