0

我尝试从我的休息客户端将文件发送到我的网络服务,但是当我确定我正在发送的媒体类型时,我不断收到 415。

我正在使用弹簧 MVC:

这里是其余客户端的代码:

public Response uploadFile(FormDataMultiPart multipart, ...) {
    WebTarget target = client.target(uri);
    Response response = target.
            path(*path*).
            request().
            header("Content-Type", "multipart/form-data").
            post(Entity.entity(multipart, MediaType.MULTIPART_FORM_DATA), Response.class);
    return response;
}

在这里我如何使用我的功能:

@RequestMapping(value = "*map path*", method = RequestMethod.POST)
public ModelAndView saveAttachedFiles(@RequestParam("file") MultipartFile myFile, Model model, HttpServletRequest request) {
    try {
     File file = new File( myFile.getOriginalFilename());
     myFile.transferTo(file);
     FileDataBodyPart filePart = new FileDataBodyPart("file", file);
     FormDataContentDisposition contentDisposition = FormDataContentDisposition.name("file").fileName(file.getName()).build();
     filePart.setContentDisposition(contentDisposition);
     FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
     formDataMultiPart.bodyPart(filePart);
     Response reponse = clientRest.uploadFile(formDataMultiPart, ...);
}

这里我的客户的请求标头(来自日志)

31 > POST ***
31 > Content-Type: multipart/form-data
31 > Cookie: $Version=1;JSESSIONID=***
31 > Referer: ***
--Boundary_1_523348906_1460533877988
Content-Type: image/jpeg
Content-Disposition: form-data; filename="Penguins.jpg"; name="file"

这里是网络服务的代码

@POST
@Path("/file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
        @FormDataParam("file") InputStream fileInputStream,
        @FormDataParam("file") FormDataContentDisposition contentDispositionHeader,
        @QueryParam("uuid") String uuid) {

    AttachedFileDto attachedFileDto;
    try {
           ** Processing the file **
        }
}

以及 Web 服务响应标头

31 < 415
31 < Access-Control-Allow-Credentials: true
31 < Access-Control-Allow-Headers: Content-Type, Accept, X-Requested-With,Cache-Control,Pragma
31 < Access-Control-Allow-Methods: GET,POST,HEAD,OPTIONS,PUT,DELETE
31 < Access-Control-Allow-Origin: http://localhost:3333
31 < Connection: Keep-Alive
31 < Content-Length: 0
31 < Content-Type: application/json
31 < Date: Wed, 13 Apr 2016 07:51:16 GMT
31 < Keep-Alive: timeout=5, max=100
31 < Server: Apache

你有什么线索吗?

谢谢 =)

4

1 回答 1

1

找到了我的问题的解决方案,我对 PathParam 和 QueryParam 感到困惑。

服务是这样的:

    @FormDataParam("file") InputStream fileInputStream,
    @FormDataParam("file") FormDataContentDisposition contentDispositionHeader,
    @QueryParam("uuid") String uuid)

我把我的 uuid 参数放在路径函数中:

public Response uploadFile(FormDataMultiPart multipart, ...) {
    WebTarget target = client.target(uri);
    Response response = target.
            path(*path*?uuid=12315465).
            request().
            header("Content-Type", "multipart/form-data").
            post(Entity.entity(multipart, MediaType.MULTIPART_FORM_DATA), Response.class);
    return response;
}

添加了 queryParam 函数:

public Response uploadFile(FormDataMultiPart multipart, ...) {
    WebTarget target = client.target(uri);
    Response response = target.
            path(*path*).
            queryParam("uuid", uuid).
            request().
            post(Entity.entity(multipart, multipart.getMediaType()));
    return response;
}

现在它起作用了=)

于 2016-04-14T13:24:11.223 回答