Poi4Xpages - 在postGeneration期间将文件写入文档

时间:2015-07-28 00:13:18

标签: apache-poi xpages lotus-domino

根据最近从Stwissel收到的建议,我使用以下代码将文件转换为mime并在POI4Xpages的后期生成过程中附加到备注文档。

编辑新代码:

以下代码附加到文档,但会引发错误:502 Bad Gateway - 服务器返回无效或不完整的响应。

public void saveExcel(Workbook a, Document newDoc) throws NotesException, IOException{

    newDoc.replaceItemValue("Form", "Provider");

    // Create the stream
    Session session = DominoUtils.getCurrentSession();
    Stream stream = session.createStream();
    // Write the workbook to a ByteArrayOutputStream
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    a.write(bos);
    // Convert the output stream to an input stream
    InputStream is = new ByteArrayInputStream(bos.toByteArray());
    stream.setContents(is);



MIMEEntity m = newDoc.createMIMEEntity("body");
MIMEHeader header = m.createHeader("content-disposition");
header.setHeaderVal("Mime attachment");
m.setContentFromBytes(stream, "application/vnd.ms-excel", MIMEEntity.ENC_IDENTITY_BINARY); 
m.decodeContent();
newDoc.save(true, true);
}

原始代码:

var stream:NotesStream = session.createStream();

// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var doc:NotesDocument = database.createDocument();
doc.replaceItemValue("Form", "Provider");
var body:NotesMIMEEntity = doc.createMIMEEntity();
var header:NotesMIMEHeader = body.createHeader("Subject");
header.setHeaderVal("MIME attachment");
if (stream.open("c:\\notes\\data\\abc.xlsx", "binary")) {
    if (stream.getBytes() != 0) {
        body.setContentFromBytes(stream, "application/vnd.ms-excel",
        NotesMIMEEntity.ENC_IDENTITY_BINARY);
    } else requestScope.status = "File was not found.";
} else requestScope.status = "Could not open file.";
stream.close();
doc.save(true, true);
// Restore conversion
session.setConvertMIME(true);

但是,此代码仅附加已存储在服务器本地目录中的文件。如何获取此代码以获取POI fileOutputStream并附加?

1 个答案:

答案 0 :(得分:1)

这是Knut和我评论过的内容的混合。重要的是,您将使用write()方法将工作簿数据传递给输出流。

// Create the stream
Stream stream = session.createStream();
// Write the workbook (you haven't clarified) to a ByteArrayOutputStream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
// Convert the output stream to an input stream
InputStream is = new ByteArrayInputStream(bos.toByteArray());
stream.setContents(is);