[英]Axis2 attachments are vanishing in the response
我正在使用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始終為null。 盡管我認為是在服務器端,但已讀取文件並將其與SOAP數據包一起附加。 難道我做錯了什么 ?
編輯:我已經將<parameter name="enableSwA" locked="false">true</parameter>
放在axis2.xml文件中。
我找到了解決此問題的方法。 問題是在服務器端,通過使用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);
其余代碼保持不變。
有關更多信息,請參見此處 。
還配置將下載附件的臨時文件夾
使用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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.