2012-05-21 3 views
3

좀 문자열 메시지를 보낼 생각 다음과 같은 클라이언트 코드와 REST 서비스를 호출하기 위해 노력하고있어 자세한 사항 X urlencoded를-WWW-폼뿐만 아니라 첨부 파일 :저지와를 통해 파일을 전송

ClientConfig config = new DefaultClientConfig(); 
config.getClasses().add(FormProvider.class); 
Client client = Client.create(config); 
WebResource webResource = client.resource("http://some.url/path1/path2"); 

File attachment = new File("./file.zip"); 

FormDataBodyPart fdp = new FormDataBodyPart(
      "content", 
      new ByteArrayInputStream(Base64.encode(FileUtils.readFileToByteArray(attachedLogs))), 
      MediaType.APPLICATION_OCTET_STREAM_TYPE); 
form.bodyPart(fdp); 

ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, form);  

내가 타겟팅하는 서버는 Base64로 인코딩 된 내용을 허용하므로 File에서 ByteArray로 추가 전송하는 이유가 여기에 있습니다.

또한 com.sun.jersey.core.impl.provider.entity.FormProvider 클래스는 "x-www-form-urlencoded"요청의 생성 및 소비에 대해주의해야합니다.

@Produces({"application/x-www-form-urlencoded", "*/*"}) 
@Consumes({"application/x-www-form-urlencoded", "*/*"}) 

하지만 여전히 나는 최종까지 다음 스택 트레이스와 함께 :

com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.sun.jersey.multipart.FormDataMultiPart, and MIME media type, application/x-www-form-urlencoded, was not found at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149) ~[jersey-client-1.9.1.jar:1.9.1] 
at com.sun.jersey.api.client.Client.handle(Client.java:648) ~[jersey-client-1.9.1.jar:1.9.1] 
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:670) ~[jersey-client-1.9.1.jar:1.9.1] 
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~[jersey-client-1.9.1.jar:1.9.1] 
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:563) ~[jersey-client-1.9.1.jar:1.9.1] 

이 하나에 어떤 도움을?

답변

4

나는 클라이언트 측에서 작동하는 것을 얻을 수 있었다. 문제는 파일을 별도의 메시지 본문 부분으로 보내고 있었지만 x-www-form-urlencoded is actually packing all of the data as parameters in the query that is the entire body이었습니다.

그래서 작업 클라이언트 코드는 것이다 뉴저지 포스트 방법을 통해 첨부 파일을 보내려면 :

아파치 커먼즈 'Base64로 인코더는 인코딩 된 바이트 배열, 확실하지으로 파일을 변환하는 내 경우에는 필요했다
ClientConfig config = new DefaultClientConfig(); 
Client client = Client.create(config); 
WebResource webResource = client.resource("http://some.url/path1/path2"); 

MultivaluedMapImpl values = new MultivaluedMapImpl(); 
values.add("filename", "report.zip"); 
values.add("text", "Test message"); 
values.add("content", new String(Base64.encode(FileUtils.readFileToByteArray(attachedLogs)))); 
ClientResponse response = webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, values); 

이것이 일반적인 요구 사항이라면.

+0

파일을 업로드 할 때 멀티 파트를 사용하는 것이 더 효율적이지만 작업이 효율적입니다. multipart를 시도하려면 accept를 "multipart/form-data"로 설정 한 다음 보내려는 모든 부분 (파일, 필드 등)에 대해 클라이언트의 FormDataMultiPart를 사용하십시오. –

+0

참고 : 필드의 경우 .field ("name", "value")를 사용하십시오. –

1

application/x-www-form-urlencoded 대신 Multipart/form-data을 사용해보세요. This tutorial 도움이 될 수 있습니다.

+0

감사합니다. Alex이지만 서버 측을 제어 할 수 없기 때문에 멀티 파트로 전환하는 것은 옵션이 아닙니다. –

관련 문제