简体   繁体   English

Axis2附件的响应消失了

[英]Axis2 attachments are vanishing in the response

I am using axis2 to come up with a basic web service which will get the file name as parameter and produces a response SOAP packet which will have the file attached along with the SOAP. 我正在使用axis2提出一个基本的Web服务,该服务将获取文件名作为参数,并生成一个响应SOAP数据包,该数据包将随SOAP一起附加文件。

Here is the way I am creating the service code (its simple and inspired by Axis2 sample code) 这是我创建服务代码的方式(它很简单,并受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;
}

Now The client side code - 现在,客户端代码-

      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);

Problem is that dataHandler is always null. 问题是dataHandler始终为null。 Though I think at the server side, the file was read and attached along with the SOAP packet. 尽管我认为是在服务器端,但已读取文件并将其与SOAP数据包一起附加。 Am I doing something wrong ? 难道我做错了什么 ?

EDIT : I have put <parameter name="enableSwA" locked="false">true</parameter> in the axis2.xml file. 编辑:我已经将<parameter name="enableSwA" locked="false">true</parameter>放在axis2.xml文件中。

I found the solution of this issue. 我找到了解决此问题的方法。 The problem was, at the server side, by using MessageContext msgCtx = MessageContext.getCurrentMessageContext(); 问题是在服务器端,通过使用MessageContext msgCtx = MessageContext.getCurrentMessageContext(); call, we get the handle of the incoming message context. 调用,我们获得传入消息上下文的句柄。 I was adding the attachment on the incoming message context, whereas, the attachment needs to be added to the outgoing message context. 我是在传入消息上下文中添加附件,而该附件需要添加到传出消息上下文中。 To get the handle of the outgoing message context, following steps needs to be done - 要获取传出消息上下文的句柄,需要完成以下步骤-

   //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);

Once the outgoing message context is received, add the attachment here - 收到外发邮件上下文后,在此处添加附件-

String attachmentID = outMessageContext.addAttachment(dataHandler);

Rest of the code remains the same. 其余代码保持不变。

More on this can be found here . 有关更多信息,请参见此处

Also configure temporary folder where attachment will be downloaded 还配置将下载附件的临时文件夹

Using axis2.xml or services.xml, 使用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>

Programmatically in the client side, 在客户端以编程方式

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM