我能够使用SoapUI附加一个完全相同的SOAP Request的zip文件,但不能使用SAAJ Api的Java。 这是我在SOAPUI和JAVA中使用的SOAP请求:
projectName
cid:myFile.zip
现在这里是我在JAVA中用来制作SOAP请求并附加文件的代码。 代码能够成功发送SOAP请求,但无法附加zip文件。
SOAPConnection connection = null;
MessageFactory factory = null;
SOAPMessage message = null;
SOAPHeader header = null;
SOAPPart soapPart = null;
SOAPEnvelope envelope = null;
SOAPElement headerElement = null;
SOAPBody body = null;
SOAPElement initialBody = null;
SOAPElement projectName = null;
SOAPElement version = null;
AttachmentPart attachment = null;
SOAPElement content = null;
SOAPConnectionFactory soapConnectionFactory = null;
MessageContext context = null;
SOAPMessage response = null;
URL endpoint = null;
try {
factory = MessageFactory.newInstance();
message = factory.createMessage();
header = message.getSOAPHeader();
soapPart = message.getSOAPPart();
//Namespaces
envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("dep", "NAMESPACE");
//SOAP Body
body = message.getSOAPBody();
projectName = body.addChildElement("projectName", "dep");
projectName.addTextNode("projectName");
//Attachment
File uploadFile = new File("myFile.zip");
DataHandler dh = new DataHandler(new FileDataSource(uploadFile));
attachment = message.createAttachmentPart(dh);
//InputStream targetStream = new FileInputStream(uploadFile);
//attachment.setRawContent(targetStream, Files.probeContentType(uploadFile.toPath()));
System.out.println("The file size is " + attachment.getSize());
attachment.setContentType("application/octet-stream");
attachment.setContentId("myFile.zip");
message.addAttachmentPart(attachment);
content = initialBody.addChildElement("content","dep");
content.addTextNode("cid:" + attachment.getContentId());
message.saveChanges();
//Response
soapConnectionFactory = SOAPConnectionFactory.newInstance();
connection = soapConnectionFactory.createConnection();
endpoint = new URL("endpoint");
response = connection.call(message, endpoint);
response.writeTo(System.out);
}
catch(Exception e) {
e.printStackTrace();
}
finally{
try {
connection.close();
}
catch(Exception e){
}
}
这一行:“response.writeTo(System.out)”产生以下输出。
The file size is 21426
S:Server
Project upload failed. No file was attached.
有没有人成功获得一个zip文件附加他们的SOAP请求? 如果有人能够使用Java,curl或其他任何东西来附加带有SOAP请求的文件,我将愿意使用不同的库。