2015-10-16 60 views
0

我在項目上使用Axis 1.4,我正在轉向Axis2 1.6.3。我因爲在Axis1.4問這是很簡單:如何使用Axis2將二進制文件發送到Web服務?

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

您只需添加一個的DataHandler的存根,然後將其發送。但在Axis2似乎這種方法不存在。所以我想知道將DataHandler附加到存根的新方法是什麼?

當我在網上搜索時,我發現你必須將DataHandler附加到MessageContextDownloading a Binary File from a Web Service using Axis2 and SOAP with Attachments)。

所以我照它說:

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。我認爲它不起作用,因爲這個片段應該在服務器端使用。我想要的是能夠將文件發送到服務器,而不是從服務器中檢索一個文件。

我可能會錯過一些東西。也許這不是這樣做,無論如何,任何幫助表示讚賞。在此期間,我會繼續在互聯網上搜索,如果我找到了一些我會讓你知道:)

回答

0

所以過了一段時間,我想出瞭如何在Axis2文檔上做到這一點。

轉至SOAP with Attachments (SwA) with Axis2並在第二部分名爲發送SwA類型附件。在這裏您將瞭解如何將文件發送到服務器。

這是代碼片段,他們提供:

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); 
} 

欲瞭解更多信息,請查看文檔的頁面。

乾杯!

相關問題