Axis2 和网络服务:文件上传



我正在尝试使用 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);

但是我在执行调用中遇到轴故障,告诉我"参数数量错误"。

我还尝试使用使用 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);

但是我得到了另一个轴错误:"org.apache.axiom.om.impl.llom.OMTextImpl不能转换为org.apache.axiom.om.OMElement"。但是我不知道我可以给生成的方法"setOmEle"提供什么参数,因为它是一种对象类型。

我认为上传文件是人们可以想象的简单服务之一.. :P我真的希望有人能给我一些建议,这个问题让我发疯了!

提前致谢:)

这实际上很简单:启用 MTOM(但不是 SwA(并使用DataHandler作为参数类型。

看看这里,但也要求考虑使用 Servlet 的 doPost; 正如线程所建议的那样 - 轴2文件按块上传

如果您还没有看到这个,那么也检查这个以获取有关您正在使用的方法的详细信息 http://axis.apache.org/axis2/java/core/docs/mtom-guide.html

安德烈亚斯的建议真的很有帮助!我尝试将字节数组传递给服务,但我遇到了一些问题,例如服务器上的文件大小与客户端上的文件大小不同。

使用数据处理程序,我没有这样的问题。我已经在 Axis 中启用了 MTOM(\WEB-INF\conf\axis2.xml(。我的服务操作签名是这样的:

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 MTOM
     SOAPBinding binding = (SOAPBinding) bp.getBinding();
     binding.setMTOMEnabled(true);
    
  6. 结果:- 使用 Sting 和字节传递 i/p,但 i/p 收到 @ 服务方法为空

相关内容

最新更新