代码之家  ›  专栏  ›  技术社区  ›  Shamik

Axis2附件在响应中消失

  •  4
  • Shamik  · 技术社区  · 15 年前

    我正在使用axis2开发一个基本的Web服务,该服务将获取文件名作为参数,并生成一个响应SOAP包,该包将文件与SOAP一起附加。

    下面是我创建服务代码的方法(它很简单,灵感来自于axis2示例代码)

    public String getFile(String name) throws IOException
    {
    MessageContext msgCtx = MessageContext.getCurrentMessageContext();
    File file = new File (name);
    System.out.println("File = " + name);
    System.out.println("File exists = " + file.exists());
    FileDataSource fileDataSource = new FileDataSource(file);
    System.out.println("fileDataSource = " + fileDataSource);
    DataHandler dataHandler = new DataHandler(fileDataSource);
    System.out.println("DataHandler = " + dataHandler);
        String attachmentID = msgCtx.addAttachment(dataHandler);
        System.out.println("attachment ID = " + attachmentID);
        return attachmentID;
    }
    

    现在客户端代码-

          MessageContext response = mepClient
                .getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
        SOAPBody body = response.getEnvelope().getBody();
        OMElement element = body.getFirstElement().getFirstChildWithName(
        new QName("http://service.soapwithattachments.sample","return"));
        String attachementId = element.getText();
        System.out.println("attachment id is " + attachementId);
        Attachments attachment = response.getAttachmentMap();
            DataHandler dataHandler = attachment.getDataHandler(attachementId);
    

    问题是数据处理程序始终为空。尽管我认为在服务器端,文件是与SOAP包一起读取和附加的。我做错什么了吗?

    编辑: 我放了 <parameter name="enableSwA" locked="false">true</parameter> 在axis2.xml文件中。

    2 回复  |  直到 14 年前
        1
  •  2
  •   Shamik    15 年前

    我找到了这个问题的解决办法。 问题是,在服务器端,使用 MessageContext msgCtx = MessageContext.getCurrentMessageContext(); 调用,我们得到传入消息上下文的句柄。我正在传入消息上下文中添加附件,而附件需要添加到传出消息上下文中。 要获取传出消息上下文的句柄,需要执行以下步骤-

       //this is the incoming message context
        MessageContext inMessageContext  = MessageContext.getCurrentMessageContext();
        OperationContext operationContext = inMessageContext.getOperationContext();
        //this is the outgoing message context
        MessageContext outMessageContext =     operationContext.getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
    

    一旦接收到传出消息上下文,请在此处添加附件-

    String attachmentID = outMessageContext.addAttachment(dataHandler);
    

    其余代码保持不变。

    可以找到更多关于这个的信息 here .

        2
  •  0
  •   Ravi Gupta    15 年前

    同时配置将下载附件的临时文件夹

    使用axis2.xml或services.xml,

    <parameter name="cacheAttachments" locked="false">true</parameter>
    <parameter name="attachmentDIR" locked="false">temp directory</parameter>
    <parameter name="sizeThreshold" locked="false">4000</parameter>
    

    在客户端以编程方式,

    options.setProperty(Constants.Configuration.CACHE_ATTACHMENTS,
                                                       Constants.VALUE_TRUE);
    options.setProperty(Constants.Configuration.ATTACHMENT_TEMP_DIR,TempDir);
    options.setProperty(Constants.Configuration.FILE_SIZE_THRESHOLD, "4000");