Axis2和Webservices:文件上传

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

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

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

public String uploadAttachment(OMElement omEle); 

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

                                                                                

客户端,我试图打电话给服务:

 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告诉我“错误的参数数量”。

我也尝试使用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”,因为它是一个Object类型。

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

提前致谢 :)

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

看看这里,还要考虑使用Servlet的doPost; 正如线程所暗示的那样 – 大块的Axis2文件上传

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

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

使用DataHandler我没有这样的问题。 我在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&byte传递i / p但接收到i / p @ service方法为空