簡體   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