좀 문자열 메시지를 보낼 생각 다음과 같은 클라이언트 코드와 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]
이 하나에 어떤 도움을?
파일을 업로드 할 때 멀티 파트를 사용하는 것이 더 효율적이지만 작업이 효율적입니다. multipart를 시도하려면 accept를 "multipart/form-data"로 설정 한 다음 보내려는 모든 부분 (파일, 필드 등)에 대해 클라이언트의 FormDataMultiPart를 사용하십시오. –
참고 : 필드의 경우 .field ("name", "value")를 사용하십시오. –