0

私は自分のレストクライアントからファイルをWebサービスに送信しようとしますが、送信しているメディアタイプを確信していれば415を取得し続けます。ここでWebサービスへSendindファイル:HTTPエラー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" 

ここでは、任意の手掛かりをWebサービス

@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 

のコードを持っていますか?

感謝=)

+2

yoあなたは正しい 'MediaType'を追加しようとしましたか?例:@RequestMapping(value = "* map path *"、method = RequestMethod.POST、consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) ' – rapasoft

答えて

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; 
} 

そして今、それは動作します=)

関連する問題