[英]Axis2 and Webservices: File Upload
I'm trying to write a simple web service using Axis2. 我正在尝试使用Axis2编写一个简单的Web服务。 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.
在一开始,我还尝试使用Java2WSDL和WSDL2Java创建WSDL文件,并且客户端希望传递java.io.File数据类型。 Of course it didn't work.
当然没有用。
I'm now trying to upload the file using SOAP attachments and MTOM or SwA. 我现在正尝试使用SOAP附件和MTOM或SwA上传文件。 I've enabled them both in axis2\\WEB-INF\\conf\\axis2.xml
我已经在axis2 \\ WEB-INF \\ conf \\ axis2.xml中启用了它们
Server side, the signature of my service operation it is: 服务器端,我的服务操作的签名是:
public String uploadAttachment(OMElement omEle);
And this is the WSDL generated using Java2WSDL tool: 这是使用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>
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);
But i get an Axis Fault on the execute call telling me "wrong number of arguments". 但是我在execute调用上遇到一个Axis Fault,告诉我“参数数量错误”。
I've also tried calling the service using the client generated with WSDL2Java: 我还尝试使用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);
But i get another Axis Fault: "org.apache.axiom.om.impl.llom.OMTextImpl cannot be cast to org.apache.axiom.om.OMElement". 但是我遇到另一个Axis Fault:“ org.apache.axiom.om.impl.llom.OMTextImpl无法转换为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..
但是我不知道我可以为生成的方法“ setOmEle”提供什么参数,因为它是对象类型。
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! 我以为上传文件是某人可以想象的简单服务之一。:P我真的希望有人可以给我一些建议,这个问题使我发疯!
thanks in advance :) 提前致谢 :)
这实际上很简单:启用MTOM(而不是SwA)并将DataHandler
用作参数类型。
Take a look here, but also would ask to think about using Servlet's doPost; 在这里看一看,但也会要求考虑使用Servlet的doPost; as the thread suggests - Axis2 File Upload by chunk
如线程所示-Axis2文件按块上传
If you have not seen this, then check this one too for details about the method you are using http://axis.apache.org/axis2/java/core/docs/mtom-guide.html 如果您还没有看到它,那么也请检查一下它,以获取有关您正在使用的方法的详细信息http://axis.apache.org/axis2/java/core/docs/mtom-guide.html
Andreas' advice was really helpful! 安德烈亚斯的建议真的很有帮助! I've tried passing an array of byte to the service, but i has some problems like the size of files on the server weren't of the same size of the files on the client..
我试图将字节数组传递给服务,但是我遇到了一些问题,例如服务器上文件的大小与客户端上文件的大小不同。
With DataHandler i haven't such problems. 使用DataHandler时,我没有这样的问题。 I've enabled MTOM in Axis (\\WEB-INF\\conf\\axis2.xml).
我已在Axis(\\ WEB-INF \\ conf \\ axis2.xml)中启用了MTOM。 My service operation signature was something like that:
我的服务操作签名是这样的:
public String importFile(String name, DataHandler dh);
And client side, after i've generated the client with WSDL2Java, i used the service as follows: 在客户端方面,在使用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);
Thanks again for your support and your advices :) 再次感谢您的支持和建议:)
I could do this successfully when I generate stubs using wsdltojava but when I tried same using wsimport command I am receiving null parameter data at server side. 当我使用wsdltojava生成存根时,我可以成功完成此操作,但是当我使用wsimport命令尝试相同的操作时,我在服务器端接收了空参数数据。
I am following below process. 我正在下面的过程。
@BindingType(value = SOAPBinding.SOAP11HTTP_MTOM_BINDING)
@BindingType(value = SOAPBinding.SOAP11HTTP_MTOM_BINDING)
编写服务端代码 Test code with with mtom enable 具有mtom enable的测试代码
//Enable MTOM SOAPBinding binding = (SOAPBinding) bp.getBinding(); binding.setMTOMEnabled(true);
Result:- Passing i/p with Sting & byte but i/p received @ service method is null 结果:-通过Sting和字节传递i / p,但在服务方法中收到的i / p为空
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.