2016-07-20 175 views
0

我需要使用上传文件API。我使用Jersy创建了客户端,它给了我适当的回应。这里是示例代码:Jersy Rest客户端到Apache CXF客户端转换

final Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build(); 
FileDataBodyPart filePart = new FileDataBodyPart("file", new File("somePath/fileName.txt")); 
FormDataMultiPart formDataMultiPart = new FormDataMultiPart(); 
FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart.field("someField", "001").bodyPart(filePart); 
final WebTarget target = client.target("http://serverUrl.com:8000/cq5/uploadApi"); 
final Response response = target.request().post(Entity.entity(multipart, multipart.getMediaType())); 

但在我的项目中,我本来应该使用CXF,我试图用CXF实现同样的事情。这是我尝试过的。

String path = "/uploadApi"; 
WebClient topicWebClient = WebClient.fromClient(webClient, true) 
     .type(MediaType.MULTIPART_FORM_DATA).path(path); 
ContentDisposition cd = new ContentDisposition("attachment;filename=fileName.txt"); 
Attachment att = new Attachment("file", new ByteArrayInputStream("testContent".getBytes()), cd); 
final Response response = topicWebClient.post(att); 

但在这里我没有得到任何回应。它继续加载。即使在我的日志中也没有得到任何错误。

我错过了什么?请帮助我得到适当的回应。

+0

你是怎么创建'webClient'的?似乎网址不正确 – pedrofb

+0

没有网址是正确的。我没有在该代码片段中添加该代码的详细信息。 – Ramesh

+0

什么是响应代码?服务器是否响应? – pedrofb

回答

1

[在评论答案]

服务器响应客户CXF 401不正当,所以它是一种身份验证问题。与Jersey客户端进行比较后,需要在请求头中添加服务器所需的凭据。

这些字段不在CXF的标题中,所以很可能是原因。在配置CXF WebClient时添加它们。

webClient.header(name,value)