在IBM Domino Server(Win32平台上的V8.5.3FP1)上,有两个Web代理,可根据请求生成PDF和RTF文件。
每个代理在临时文件夹中生成RTF或PDF文件,然后打开OutputStream
实例将此文件写入客户端(浏览器,出现保存文件对话框时)。
一切正常。文件生成并正确保存在临时文件夹中。但是将这些文件写入OutputStream以允许用户将其保存到本地磁盘,它无法正常工作。有些文件写得很好(小文件,~11Kb),但更大的文件,~34K部分保存(有时保存276个字节,有时保存4K字节等)。
我在我的代理中获得OutputStream,如下所示:
final OutputStream os = this.getAgentOutputStream();
生成并保存文件时我使用:
final FileInputStream fis = new FileInputStream(pdfFilePath);
IOUtils.copy(fis, os); // it is from Apache Commons IOUtils
fis.close();
不起作用。
然后我用这种方式代替:
final byte[] resultArray = FileUtils.readFileToByteArray(new File(pdfFilePath)); // result array has correct length and correct contents
os.write(resultArray);
os.flush();
os.close();
不起作用。
然后我用这种方式代替(棘手,但仅用于实验目的):
final byte[] resultArray = FileUtils.readFileToByteArray(new File(pdfFilePath)); // result array has correct length and correct contents
for (byte a:resultArray) {
os.write(a);
}
os.flush();
os.close();
一样。不。工作
在将数据发送到输出流之前,我已调用:
java.io.PrintWriter pw = this.getAgentOutput();
pw.println("Content-type: application/pdf"); // also tried octet-stream, no effect
pw.println("Content-Disposition: attachment; filename=\"file.pdf\"");
我的问题如下,伙计们。我的做法有什么问题?我在这做错了什么?文件已正确创建并保存在服务器上。输出流正确打开,文件读取正确。当我写输出流时,没有例外。输出流正确刷新和关闭。
有什么问题?我想整天解决这个问题,但我没有找到线索。
有什么想法吗?
答案 0 :(得分:1)
似乎Domino有代理OutputStream的错误。通过agentRef.getAgentOutputStream();
获得的流无法正常工作并执行部分写入。
我没有使用这种方式,而是决定将文件附加到NotesDocument实例,保存它并为用户提供本文档中附加文件的链接。
答案 1 :(得分:0)
这也使我感到沮丧,但幸运的是我能够找到另一种方法。
https://www.linkedin.com/pulse/writing-binary-data-from-notes-agent-arun-shankar
步骤1:将字节数组转换为JSON数组字符串。
编写一个循环以使用字节数组创建JSON数组字符串。
byte[] pdfFile = pdf.getPDF();
ByteBuffer byteBuffer = ByteBuffer.wrap(pdfFile);
CharBuffer result = Charset.forName("UTF-8").decode(byteBuffer);
PrintWriter agentPrintWriter = getAgentOutput();
agentPrintWriter.println("Content-type:application/pdf");
agentPrintWriter.println("Content-Disposition:attachment;filename=\"Report.pdf\"\n");
StringBuilder buffer = new StringBuilder();
buffer.append("[");
for(int index=0;index<pdfFile.length;index++)
{
buffer.append(""+pdfFile[index]);
if(index<pdfFile.length-1)
buffer.append(",");
}
buffer.append("]");
agentPrintWriter.println(buffer.toString());
上面应该生成一个表示JSON数组的String,如下所示。
“[37,80,68,70,45,49,46,52,10,49,32,48,32,111,98,106,10,60,60,...,10]”
使用代理的PrintWriter实例编写此字符串。
步骤2:使用收到的数据创建Blob并打开它是附件
客户端收到的数据是JSON字符串。 这可以转换为字节数组,并通过从中创建Blob对象作为附件下载。
//Data Received from the AJAX Request
var byteArray = new Uint8Array(JSON.parse(data));
var blob = new Blob([byteArray], {type: "application/pdf"});
var blobUrl = URL.createObjectURL(blob);
window.open(blobUrl)
我仅在Google Chrome中测试了该解决方案,但我很确定,它也适用于其他浏览器。