Привет, я работаю над загрузкой api в моем проекте. Я могу отправить данные и получить ответ, используя jersy client.Как отправить multipart/formData при загрузке файла после вызова в Apache - клиент CXF?
Я получаю 200 в качестве ответа. Вот этот код.
final Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
client.register(new LoggingFilter());
FileDataBodyPart filePart = new FileDataBodyPart("file", new File("/Users/rasaminathan/Desktop/test.txt"));
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart.field("param1", "paramValue")
.field("param2", "value")
.bodyPart(filePart);
final WebTarget target = client.target("http://URL:8000/apiPath");
final Response response = target.request().post(Entity.entity(multipart, multipart.getMediaType()));
И это журнал, который я получил.
Jul 22, 2016 12:15:14 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Sending client request on thread main
1 > POST http://URL:8000/apiPath
1 > Content-Type: multipart/form-data
Jul 22, 2016 12:15:15 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Client response received on thread main
1 < 200
1 < Content-Length: 203
1 < Content-Type: application/json
1 < Date: Fri, 22 Jul 2016 06:45:14 GMT
1 < X-Powered-By: Servlet/2.5 JSP/2.1
Но в моем проекте мы должны использовать CXF. Я пробовал то же самое с веб-клиентом CXF. Но я получаю «401» Несанкционированную ошибку. Вот код.
WebClient client = WebClient.create("http://URL:8000");
String path = "apiPath";
client.type(MediaType.MULTIPART_FORM_DATA)
.path(path);
ClientConfiguration config = WebClient.getConfig(client);
config.getInInterceptors().add(new LoggingInInterceptor());
config.getOutInterceptors().add(new LoggingOutInterceptor());
List<Attachment> atts = new LinkedList<Attachment>();
atts.add(new Attachment("file", "application/octet-stream", new ByteArrayInputStream("testContent".getBytes())));
atts.add(new Attachment("param1","text/plain","paramValue"));
atts.add(new Attachment("param2","text/plain","value"));
MultipartBody body = new MultipartBody(atts);
Response response= client.post(body);
И это журнал, который я получил.
Jul 22, 2016 12:23:41 PM org.apache.cxf.interceptor.LoggingOutInterceptor
INFO: Outbound Message
---------------------------
ID: 1
Address: http://URL:8000/apiPath
Http-Method: POST
Content-Type: multipart/form-data; boundary="uuid:14597725-d376-4643-92f9-7a4a64ae1054"
Headers: {Accept=[*/*]}
Payload: --uuid:14597725-d376-4643-92f9-7a4a64ae1054
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <file>
testContent
--uuid:14597725-d376-4643-92f9-7a4a64ae1054
Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-ID: <param1>
paramValue
--uuid:14597725-d376-4643-92f9-7a4a64ae1054
Content-Type: text/plain
Content-Transfer-Encoding: binary
Content-ID: <param2>
value
--------------------------------------
Jul 22, 2016 12:23:42 PM org.apache.cxf.interceptor.LoggingInInterceptor
INFO: Inbound Message
----------------------------
ID: 1
Response-Code: 401
Encoding: UTF-8
Content-Type: text/html; charset=UTF-8
Headers: {Content-Length=[46], content-type=[text/html; charset=UTF-8], Date=[Fri, 22 Jul 2016 06:53:42 GMT], X-Powered-By=[Servlet/2.5 JSP/2.1]}
--------------------------------------
Я думаю, что значения параметров Param1, Param2 не отправлены должным образом. Вот почему я получаю ошибку 401, я думаю. Я правильно отправляю данные в веб-клиенте CXF? Если нет, пожалуйста, помогите мне исправить код.