Axis2和Webservices:文件上传 [英] Axis2 and Webservices: File Upload

查看:187
本文介绍了Axis2和Webservices:文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Axis2编写一个简单的Web服务。它的行为非常简单:它在输入中获取一个文件并存储它。
我已经尝试了几件事来做这个简单的文件上传服务。最初,我还尝试使用Java2WSDL和WSDL2Java来创建WSDL文件,并希望传递java.io.File数据类型的客户端。当然它不起作用。

I'm trying to write a simple web service using Axis2. Its behaviour is really simple: it takes a file in input and stores it. I've tried several things to do this "simple" file upload service. At the beginning i also tried to use Java2WSDL and WSDL2Java to create the WSDL file and the client hoping to pass the java.io.File datatype. Of course it didn't work.

我现在正在尝试使用SOAP附件和MTOM或SwA上传文件。
我已经在axis2 \WEB-INF \ conf \axis2.xml中启用了它们

I'm now trying to upload the file using SOAP attachments and MTOM or SwA. I've enabled them both in axis2\WEB-INF\conf\axis2.xml

服务器端,我的服务操作的签名吧是:

Server side, the signature of my service operation it is:

public String uploadAttachment(OMElement omEle);

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

And this is the WSDL generated using Java2WSDL tool:

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

客户端,我试图拨打该服务:

Client side, i've tried to call the service:

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

但我在执行调用中得到一个Axis Fault告诉我参数数量错误。

But i get an Axis Fault on the execute call telling me "wrong number of arguments".

我也尝试使用用WSDL2Java生成的客户端来调用服务:

I've also tried calling the service using the client generated with 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故障:org.apache.axiom.om.impl.llom.OMTextImpl无法转换为org.apache.axiom.om.OMElement。
但我不知道我可以作为生成方法setOmEle的参数给出什么,因为它是一个Object类型..

But i get another Axis Fault: "org.apache.axiom.om.impl.llom.OMTextImpl cannot be cast to org.apache.axiom.om.OMElement". But i don't know what i can give as a parameter to the generated method "setOmEle" since it is an Object type..

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

i thought to upload a file was one of the simples services that someone can imagine.. :P i really hope someone can give me some advice, this problem is making me crazy!

提前感谢:)

推荐答案

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

It is actually simple: enable MTOM (but not SwA) and use DataHandler as the argument type.

这篇关于Axis2和Webservices:文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆