繁体   English   中英

如何使用Axis2将二进制文件发送到Web服务?

[英]How do you send a binary file to a webservice using Axis2?

我在一个项目上使用了Axis 1.4 ,并且正在使用Axis2 1.6.3 我问这个问题是因为在Axis1.4中,它非常简单:

myStub.addAttachment(new DataHandler(new FileDataSource("path_to_file")));

您只需将DataHandler添加到存根,然后发送它即可。 但是在Axis2中 ,似乎这种方法不存在。 所以我想知道将DataHandler附加到存根的新方法是什么?

在Internet上搜索时,我发现您必须将DataHandler附加到MessageContext使用Axis2和带有附件的SOAP从Web服务下载二进制文件 )。

所以我做到了据说:

MessageContext messageContext = MessageContext.getCurrentMessageContext();
OperationContext operationContext = messageContext.getOperationContext(); 
MessageContext outMessageContext = operationContext.getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
outMessageContext.addAttachment(new DataHandler(new FileDataSource("path_to_file")));

但是问题是MessageContext.getCurrentMessageContext()返回null。 我认为它不起作用,因为应该在服务器端使用此代码段。 我想要的是能够将文件发送到服务器,而不是从服务器检索文件。

我可能会缺少一些东西。 也许这不是做到这一点的方法,无论如何,我们都会感谢您的帮助。 在此期间,我将继续在互联网上搜索,如果发现了一些问题,我会通知您:)

所以过了一段时间,我在Axis2 Documentation上找到了解决方法。

使用Axis2转到SOAP with Attachments(SwA),在第二部分中,该字段名为Sending SwA Type Attachments 在这里,您将了解如何将文件发送到服务器。

这是他们提供的代码片段:

public void uploadFileUsingSwA(String fileName) throws Exception {

    Options options = new Options();
    options.setTo(targetEPR);
    options.setProperty(Constants.Configuration.ENABLE_SWA, Constants.VALUE_TRUE);
    options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
    options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
    options.setTo(targetEPR);

    ServiceClient sender = new ServiceClient(null,null);
    sender.setOptions(options);
    OperationClient mepClient = sender.createClient(ServiceClient.ANON_OUT_IN_OP);

    MessageContext mc = new MessageContext();   
    mc.setEnvelope(createEnvelope());
    FileDataSource fileDataSource = new FileDataSource("test-resources/mtom/test.jpg");
    DataHandler dataHandler = new DataHandler(fileDataSource);
    mc.addAttachment("FirstAttachment",dataHandler);

    mepClient.addMessageContext(mc);
    mepClient.execute(true);
}

有关更多信息,请查看文档页面。

干杯!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM