fjwpsyb 2021-08-26 20:00 采纳率: 0%
浏览 50
已结题

axis2 多附件调用失败,单附件传输正常


我的调用为:

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import org.apache.axiom.attachments.utils.IOUtils;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

public class FileTransferClient {
   private static EndpointReference targetEPR =
new EndpointReference("http://xxx/uip/services/FileOperation");
  
   public static boolean upload(String fileName, File file, String fileType) {
     try {
      OMElement data = buildUploadEnvelope(fileName, file, fileType);
      Options options = buildOptions();
      ServiceClient sender = new ServiceClient();
      sender.setOptions(options);
      System.out.println("The data in method upload: "+data);
      OMElement ome = sender.sendReceive(data);
      System.out.println("Convert the data to element in method upload: "+ome);
      String b = ome.getText();
      return Boolean.parseBoolean(b);
     }
     catch(Exception e) {
       e.printStackTrace();
     }
     return false;
   }
  
   public static boolean download(String userName, String fileName, String fileType) {
     try {
       OMElement data = buildDownloadEnvelope(userName, fileName, fileType);
       Options options = buildOptions();
       ServiceClient sender = new ServiceClient();
       sender.setOptions(options);
       System.out.println("The data in method download: "+data);
       OMElement ome = sender.sendReceive(data);
       System.out.println("Convert the data to element in method download: "+ome);
       OMText binaryNode = (OMText) ome.getFirstOMChild();
       binaryNode.setOptimize (true); // 必须加此句,否则会出现 ContentID is null 的异常!
       DataHandler actualDH = (DataHandler) binaryNode.getDataHandler();
       FileOutputStream imageOutStream = new FileOutputStream("D:/userTemp/xx.gif");
       InputStream is = actualDH.getInputStream();
       imageOutStream.write(IOUtils.getStreamAsByteArray(is));
       return true;
      }
      catch(Exception e) {
        e.printStackTrace();
      }
     return false;
   }
  
   private static OMElement buildUploadEnvelope(String fileName, File file, String fileType) {
     OMFactory fac = OMAbstractFactory.getOMFactory();
     OMNamespace omNs = fac.createOMNamespace("http://impl.server.webservice.docentre.eetrust.com", "fd");
     OMElement data = fac.createOMElement("upload", omNs);
     OMElement fileContent = fac.createOMElement("fileContent", omNs);
//      OMElement aaa = fac.createOMElement("aaa", omNs);
//      fileContent.setText("aaa");
//      OMElement bbb = fac.createOMElement("bbb", omNs);
     FileDataSource dataSource = new FileDataSource(file);
     DataHandler expectedDH = new DataHandler(dataSource);
     OMText textData = fac.createOMText(expectedDH, true);
     fileContent.addChild(textData);
     FileDataSource dataSources = new FileDataSource ("D:/ 工作记录.txt");// 测试用的
     DataHandler expectedDHs = new DataHandler(dataSources);
     OMText textDatas = fac.createOMText(expectedDHs, true);
     fileContent.addChild(textDatas);
    
     OMElement _fileName = fac.createOMElement("fileName", omNs);
     _fileName.setText(fileName);
     OMElement _fileType = fac.createOMElement("fileType", omNs);
     _fileType.setText(fileType);
     data.addChild(_fileName);
     data.addChild(_fileType);
     data.addChild(fileContent);
     return data;
   }
  
   private static OMElement buildDownloadEnvelope(String userName, String fileName, String fileType) {
     OMFactory fac = OMAbstractFactory.getOMFactory();
     OMNamespace omNs = fac.createOMNamespace("http://example.org/filedata", "fd");
     OMElement data = fac.createOMElement("download", omNs);
     OMElement _userName = fac.createOMElement("userName", omNs);
     _userName.setText(userName);
     OMElement _fileName = fac.createOMElement("fileName", omNs);
     _fileName.setText(fileName);
     OMElement _fileType=fac.createOMElement("fileType", omNs);
     _fileType.setText(fileType);
     data.addChild(_userName);
     data.addChild(_fileName);
     data.addChild(_fileType);
     return data;
   }
   private static Options buildOptions() throws AxisFault {
     Options options = new Options();
     options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
     options.setTo(targetEPR);
     // enabling MTOM in the client side
     options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
     options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
     return options;
   }
   public static void main(String agrs[]) {
     String file = "D:/ 常用文件.txt";
     String fn = "常用文件";
     String ft="txt";
     boolean rtv = upload(fn,new File(file),ft);
     System.out.println("is upload success: "+rtv);

   }

}
服务端代码为:
      public OMElement upload(OMElement element) throws Exception {
              OMElement _fileContent = null;// 文件内容
              OMElement _fileName = null;// 文件名
              OMElement _fileType = null;// 文件类型
//              System.out.println("The element for upload: " + element);
              for (Iterator _iterator = element.getParent().getChildren(); _iterator.hasNext();) {
                     OMElement _ele = (OMElement) _iterator.next();
                     if (_ele.getLocalName().equalsIgnoreCase("fileContent")) {
                            _fileContent = _ele;
                     }
                     if (_ele.getLocalName().equalsIgnoreCase("fileName")) {
                            _fileName = _ele;
                     }
                     if (_ele.getLocalName().equalsIgnoreCase("fileType")) {
                            _fileType = _ele;
                     }
              }

              if (_fileContent == null || _fileType == null) {
                     throw new AxisFault("Either Image or FileName is null");
              }

              OMText binaryNode = (OMText) _fileContent.getFirstOMChild();
              String fileName = _fileName.getText();
              String fileType = _fileType.getText();
              String storeDir = TMP_PATH + "/" + "tempTest";
              File dir = new File(storeDir);
              if (!dir.exists()) {
                     dir.mkdir();
              }
              String filePath = storeDir + "/" + fileName + "." + fileType;
              File uploadFile = new File(filePath);
              if (uploadFile.exists()) {
                     filePath = storeDir + "/" + fileName + "(1)" + "." + fileType;
                     uploadFile = new File(filePath);
              }

              // Extracting the data and saving
              DataHandler actualDH;
              actualDH = (DataHandler) binaryNode.getDataHandler();

              FileOutputStream imageOutStream = new FileOutputStream(uploadFile);
              InputStream is = actualDH.getInputStream();
              imageOutStream.write(IOUtils.getStreamAsByteArray(is));
              // setting response
              OMFactory fac = OMAbstractFactory.getOMFactory();
              OMNamespace ns = fac.createOMNamespace("http://example.org/filedata",
                            "fd");
              OMElement ele = fac.createOMElement("response", ns);
              ele.setText("true");
              return ele;
       }
出错内容为:
org.apache.axis2.AxisFault: javax.xml.stream.XMLStreamException: Expected xop:Include as the sole child of an element information item (see section 3.2 of http://www.w3.org/TR/xop10/)
at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:531)
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:375)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:421)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:555)
at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:531)
at FileTransferClient.upload(FileTransferClient.java:33)
at FileTransferClient.main(FileTransferClient.java:121)

如果是单附件调用正常,多附件调用则抛出上述异常。

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-08-26 20:05
    关注

    你想联系他,在他博文下面留言评论,或者私信他。在这里提问是找不到人的。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月14日
  • 修改了问题 8月27日
  • 修改了问题 8月26日
  • 修改了问题 8月26日
  • 展开全部

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题