簡體   English   中英

Axis2和Webservices:文件上傳

[英]Axis2 and Webservices: File Upload

我正在嘗試使用Axis2編寫一個簡單的Web服務。 它的行為非常簡單:它將輸入中的文件保存起來。 我已經嘗試了幾種方法來執行此“簡單”文件上傳服務。 在一開始,我還嘗試使用Java2WSDL和WSDL2Java創建WSDL文件,並且客戶端希望傳遞java.io.File數據類型。 當然沒有用。

我現在正嘗試使用SOAP附件和MTOM或SwA上傳文件。 我已經在axis2 \\ WEB-INF \\ conf \\ axis2.xml中啟用了它們

服務器端,我的服務操作的簽名是:

public String uploadAttachment(OMElement omEle);

這是使用Java2WSDL工具生成的WSDL:

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xsd="http://services.italsystem.it" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://services.italsystem.it">
<wsdl:types>
    <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://services.italsystem.it">
        <xs:element name="uploadAttachment">
            <xs:complexType>
                <xs:sequence>
                    <xs:element minOccurs="0" name="omEle" nillable="true" type="xs:anyType"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="uploadAttachmentResponse">
            <xs:complexType>
                <xs:sequence>
                    <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
</wsdl:types>
<wsdl:message name="uploadAttachmentRequest">
    <wsdl:part name="parameters" element="xsd:uploadAttachment"/>
</wsdl:message>
<wsdl:message name="uploadAttachmentResponse">
    <wsdl:part name="parameters" element="xsd:uploadAttachmentResponse"/>
</wsdl:message>
<wsdl:portType name="ImportServicePortType">
    <wsdl:operation name="uploadAttachment">
        <wsdl:input message="xsd:uploadAttachmentRequest" wsaw:Action="urn:uploadAttachment"/>
        <wsdl:output message="xsd:uploadAttachmentResponse" wsaw:Action="urn:uploadAttachmentResponse"/>
    </wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ImportServiceSoap11Binding" type="xsd:ImportServicePortType">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <wsdl:operation name="uploadAttachment">
        <soap:operation soapAction="urn:uploadAttachment" style="document"/>
        <wsdl:input>
            <soap:body use="literal"/>
        </wsdl:input>
        <wsdl:output>
            <soap:body use="literal"/>
        </wsdl:output>
    </wsdl:operation>
</wsdl:binding>
<wsdl:binding name="ImportServiceSoap12Binding" type="xsd:ImportServicePortType">
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <wsdl:operation name="uploadAttachment">
        <soap12:operation soapAction="urn:uploadAttachment" style="document"/>
        <wsdl:input>
            <soap12:body use="literal"/>
        </wsdl:input>
        <wsdl:output>
            <soap12:body use="literal"/>
        </wsdl:output>
    </wsdl:operation>
</wsdl:binding>
<wsdl:binding name="ImportServiceHttpBinding" type="xsd:ImportServicePortType">
    <http:binding verb="POST"/>
    <wsdl:operation name="uploadAttachment">
        <http:operation location="uploadAttachment"/>
        <wsdl:input>
            <mime:content type="application/xml" part="parameters"/>
        </wsdl:input>
        <wsdl:output>
            <mime:content type="application/xml" part="parameters"/>
        </wsdl:output>
    </wsdl:operation>
</wsdl:binding>
<wsdl:service name="ImportService">
    <wsdl:port name="ImportServiceHttpSoap11Endpoint" binding="xsd:ImportServiceSoap11Binding">
        <soap:address location="http://localhost:8080/axis2/services/ImportService"/>
    </wsdl:port>
    <wsdl:port name="ImportServiceHttpSoap12Endpoint" binding="xsd:ImportServiceSoap12Binding">
        <soap12:address location="http://localhost:8080/axis2/services/ImportService"/>
    </wsdl:port>
    <wsdl:port name="ImportServiceHttpEndpoint" binding="xsd:ImportServiceHttpBinding">
        <http:address location="http://localhost:8080/axis2/services/ImportService"/>
    </wsdl:port>
</wsdl:service>
</wsdl:definitions>

客戶端,我嘗試調用該服務:

Options options = new Options();
options.setTo(new EndpointReference("http://localhost:8080/axis2/services/ImportModule"));
options.setProperty(Constants.Configuration.ENABLE_SWA, Constants.VALUE_TRUE);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);

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

MessageContext mc = new MessageContext();
SOAPFactory factory = OMAbstractFactory.getSOAP12Factory();
SOAPEnvelope env = factory.getDefaultEnvelope();
mc.setEnvelope(env);
FileDataSource fileDataSource = new FileDataSource(new File("c:\\test.jpg"));
DataHandler dataHandler = new DataHandler(fileDataSource);
mc.addAttachment("FirstAttachment",dataHandler);

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

但是我在execute調用上遇到一個Axis Fault,告訴我“參數數量錯誤”。

我還嘗試使用WSDL2Java生成的客戶端調用服務:

ImportServiceStub stub = new ImportServiceStub("http://localhost:8080/axis2/services/ImportModule");
UploadAttachment ua = new UploadAttachment();
FileDataSource fileDataSource = new FileDataSource(new File("c:\\test.jpg"));
DataHandler dataHandler = new DataHandler(fileDataSource);
ua.setOmEle(dataHandler);

UploadAttachmentResponse res = stub.uploadAttachment(ua);

但是我遇到另一個Axis Fault:“ org.apache.axiom.om.impl.llom.OMTextImpl無法轉換為org.apache.axiom.om.OMElement”。 但是我不知道我可以為生成的方法“ setOmEle”提供什么參數,因為它是對象類型。

我以為上傳文件是某人可以想象的簡單服務之一。:P我真的希望有人可以給我一些建議,這個問題使我發瘋!

提前致謝 :)

這實際上很簡單:啟用MTOM(而不是SwA)並將DataHandler用作參數類型。

在這里看一看,但也會要求考慮使用Servlet的doPost; 如線程所示-Axis2文件按塊上傳

如果您還沒有看到它,那么也請檢查一下它,以獲取有關您正在使用的方法的詳細信息http://axis.apache.org/axis2/java/core/docs/mtom-guide.html

安德烈亞斯的建議真的很有幫助! 我試圖將字節數組傳遞給服務,但是我遇到了一些問題,例如服務器上文件的大小與客戶端上文件的大小不同。

使用DataHandler時,我沒有這樣的問題。 我已在Axis(\\ WEB-INF \\ conf \\ axis2.xml)中啟用了MTOM。 我的服務操作簽名是這樣的:

public String importFile(String name, DataHandler dh);

在客戶端方面,在使用WSDL2Java生成客戶端之后,我按如下方式使用了該服務:

ImportServiceStub stub = new ImportServiceStub("http://localhost:8080/axis2/services/ImportModule");
ImportFile importFile = new ImportFile(); //operation name
DataSource fds = new FileDataSource(new File("FileName"));
importFile.setName("FileName");
importFile.setDh(new DataHandler(fds));
stub.importFile(importFile);

再次感謝您的支持和建議:)

當我使用wsdltojava生成存根時,我可以成功完成此操作,但是當我使用wsimport命令嘗試相同的操作時,我在服務器端接收了空參數數據。

我正在下面的過程。

  1. 使用jax ws規范和mtom soap 11綁定@BindingType(value = SOAPBinding.SOAP11HTTP_MTOM_BINDING)編寫服務端代碼
  2. 生成.aar文件以將其上傳到axis2服務器(axis1.6.2和tomcat 6/7)
  3. 從axis2服務器生成wsdl文件,僅保留服務名稱。
  4. 使用wsimport -keep -verbose wsdl url生成的存根
  5. 具有mtom enable的測試代碼

      //Enable MTOM SOAPBinding binding = (SOAPBinding) bp.getBinding(); binding.setMTOMEnabled(true); 
  6. 結果:-通過Sting和字節傳遞i / p,但在服務方法中收到的i / p為空

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM