问题
In our enterprise application we need to attach files to a document. We have the filename and the content of the file in a byte array. I found a solution to attach a file to a document with MIMEs:
final MIMEEntity body = document.createMIMEEntity(fileName);
final MIMEHeader bodyHeader = body.createHeader("Content-Disposition");
final boolean isHeaderValSet = bodyHeader.setHeaderVal("attachment; filename=\"" + fileName + "\"");
if (!isHeaderValSet) {
throw new ComponentException("Could not set MIME header value.");
}
body.setContentFromBytes(fileContentOutput, mimeType, MIMEEntity.ENC_IDENTITY_BINARY);
final boolean saveSuccessful = document.save();
if (!saveSuccessful) {
throw new Exception("Cannot attach file " + fileName + "to document: " + documentUniversalId);
}
This method seems to work for a file, but when I try to upload another one I get the following exception:
NotesException: Item body already exists
Is there a way to attach multiple files to a document, when you only have the name of the file and the contents in a byte array?
回答1:
Keep the first line where you create the "parent" MIMEEntity called body. Then in a loop, create child MIMEentities for all the files you wish to include:
final MIMEEntity child = body.CreateChildEntity;
child.setHeaderVal("attachment; filename=\"" + fileName + "\"");
child.createHeader("Content-Disposition");
child.setContentFromBytes(fileContentOutput, mimeType, MIMEEntity.ENC_IDENTITY_BINARY);
CreateChildEntity
来源:https://stackoverflow.com/questions/11294991/attaching-multiple-files-through-mimes-to-a-document-in-lotus-domino