Я использую axis2 для создания базового веб-сервиса, который получит имя файла в качестве параметра и создаст ответный 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. Я что-то не так делаю?
РЕДАКТИРОВАТЬ:
Я поместил <parameter name="enableSwA" locked="false">true</parameter>
в файл axis2.xml.