如何在 Apache - CXF 客户端的上传文件 post 调用中发送 multipart/formData?
How to send multipart/formData in upload file post call in Apache - CXF client?
您好,我正在我的项目中上传 api。我可以使用 jersy 客户端发送数据并获得响应。
我得到 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 webclient 进行了同样的尝试。但我收到“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 webclient 中正确发送数据?如果不是请帮我更正代码
在所有附件中设置内容处置有助于解决问题。我找到了解决方案 here.
正如我猜测的那样,参数 1、参数 2 值未到达服务器。
List<Attachment> atts = new LinkedList<Attachment>();
ContentDisposition cd = new ContentDisposition("form-data; name=\"file\";filename=\"test.txt\"");
atts.add(new Attachment("file", new ByteArrayInputStream("testContent".getBytes()),cd));
ContentDisposition cd1 = new ContentDisposition("form-data; name=\"param1\";");
atts.add(new Attachment("param1",new ByteArrayInputStream("paramValue".getBytes()),cd1));
ContentDisposition cd2 = new ContentDisposition("form-data; name=\"param2\";");
atts.add(new Attachment("param2",new ByteArrayInputStream("value".getBytes()),cd1));
MultipartBody body = new MultipartBody(atts);
Response response= client.post(body);
现在我收到了成功响应
您好,我正在我的项目中上传 api。我可以使用 jersy 客户端发送数据并获得响应。
我得到 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 webclient 进行了同样的尝试。但我收到“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 webclient 中正确发送数据?如果不是请帮我更正代码
在所有附件中设置内容处置有助于解决问题。我找到了解决方案 here.
正如我猜测的那样,参数 1、参数 2 值未到达服务器。
List<Attachment> atts = new LinkedList<Attachment>();
ContentDisposition cd = new ContentDisposition("form-data; name=\"file\";filename=\"test.txt\"");
atts.add(new Attachment("file", new ByteArrayInputStream("testContent".getBytes()),cd));
ContentDisposition cd1 = new ContentDisposition("form-data; name=\"param1\";");
atts.add(new Attachment("param1",new ByteArrayInputStream("paramValue".getBytes()),cd1));
ContentDisposition cd2 = new ContentDisposition("form-data; name=\"param2\";");
atts.add(new Attachment("param2",new ByteArrayInputStream("value".getBytes()),cd1));
MultipartBody body = new MultipartBody(atts);
Response response= client.post(body);
现在我收到了成功响应