Axis2附件在响应中消失

4
我正在使用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);

问题是dataHandler始终为空。尽管我认为在服务器端,文件已经被读取并与SOAP数据包一起附加。我做错了什么吗?

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

2个回答

2
我找到了这个问题的解决方案。 问题在于服务器端,通过使用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);

其余的代码保持不变。

更多相关信息可在这里找到。


0

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

可以使用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");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接