gpt4 book ai didi

java - 更新 AttachmentPart 内容而不创建新消息

转载 作者:行者123 更新时间:2023-12-01 11:51:55 24 4
gpt4 key购买 nike

我需要更新 SOAPMessage 中的 AttachmentPart 内容,如下图所示。我需要保持标题相同。
是否可以在不创建新的 SOAP 消息的情况下完成此操作?我正在使用 SAAJ API。

enter image description here

最佳答案

您能否使用 SOAPMessage.getAttachments() 调用(该调用返回所有附件部分的迭代器)将附件拉入新对象,进行必要的修改,然后调用 SOAPMessage.removeAllAttachments() 函数来清除对象从原始消息中调用 addAttachmentPart(AttachmentPart) 函数来重新添加更改的对象?

        SOAPMessage message = getSoapMessageFromString(foo);

List<AttachmentPart> collectionOfAttachments = new ArrayList<AttachmentPart>();

for (Iterator attachmentIterator = message.getAttachments(); attachmentIterator.hasNext()) {
AttachmentPart attachment = (AttachmentPart) attachmentIterator.next();
//**DO WORK HERE ON attachment**
collectionOfAttachments.add(attachment);
}

message.removeAllAttachments();

for (AttachmentPart newAttachment : collectionOfAttachments) {
message.addAttachmentPart(newAttachment);
}



// This method takes an XML string as input and uses it to create a new
// SOAPMessage object
// and then returns that object for further use.
private static SOAPMessage getSoapMessageFromString(String xml)
throws SOAPException, IOException {

MessageFactory factory = MessageFactory.newInstance();

// Create a new message object with default MIME headers and the data
// from the XML string we passed in
SOAPMessage message = factory
.createMessage(
new MimeHeaders(),
new ByteArrayInputStream(xml.getBytes(Charset
.forName("UTF-8"))));
return message;
}

您希望对附件进行哪些更改?将主体保留在 DOM 对象中并一起创建一个新的 SOAPMessage 会更容易吗?

关于java - 更新 AttachmentPart 内容而不创建新消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28735853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com