Xpages SSJS - 大文件的输出文件速度很慢
Xpages SSJS - Outputting files is slow for large files
我正在尝试使用 Steve Wissel 页面上的说明输出我在 domino 服务器的本地文件系统上的 pdf 文件。
http://www.wissel.net/blog/d6plinks/shwl-7mgfbn
文件将会下载,但对于 20MB 范围内的文件,需要几分钟的时间。有没有办法加快流式传输?
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
<xp:this.beforeRenderResponse><![CDATA[#{javascript://importPackage(java.io.File);
var exCon = facesContext.getExternalContext();
var response = exCon.getResponse();
var out = response.getOutputStream();
if (out==null) {
print("The freakn' stream isn't there");
} else {
print("All good with the stream");
}
try {
/*
* Move all your existing code here...
*/
print("setting headers");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename=ebook.pdf");
response.setHeader("Cache-Control", "no-cache");
var file:java.io.File = new java.io.File("C:\Autotrader\r-gswob.pdf");
if (file.exists()) {
print("File Exists");
} else {
print("file missing");
}
var fileIn:java.io.FileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(file));
var c:int;
while ((c = fileIn.read()) != -1) {
out.write(c);
}
} catch (e) {
print("Error generating dynamic PDF: " + e.toString());
} finally {
if (fileIn != null) {
fileIn.close();
}
if (out != null) {
out.close();
}
facesContext.responseComplete();
}
}]]></xp:this.beforeRenderResponse></xp:view>
使用比仅一个 int 值更大的缓冲区来复制文件:
var buffer = new byte[10000];
var len;
while ((len = fileIn.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
这会大大加快下载速度。
我正在尝试使用 Steve Wissel 页面上的说明输出我在 domino 服务器的本地文件系统上的 pdf 文件。 http://www.wissel.net/blog/d6plinks/shwl-7mgfbn
文件将会下载,但对于 20MB 范围内的文件,需要几分钟的时间。有没有办法加快流式传输?
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
<xp:this.beforeRenderResponse><![CDATA[#{javascript://importPackage(java.io.File);
var exCon = facesContext.getExternalContext();
var response = exCon.getResponse();
var out = response.getOutputStream();
if (out==null) {
print("The freakn' stream isn't there");
} else {
print("All good with the stream");
}
try {
/*
* Move all your existing code here...
*/
print("setting headers");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename=ebook.pdf");
response.setHeader("Cache-Control", "no-cache");
var file:java.io.File = new java.io.File("C:\Autotrader\r-gswob.pdf");
if (file.exists()) {
print("File Exists");
} else {
print("file missing");
}
var fileIn:java.io.FileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(file));
var c:int;
while ((c = fileIn.read()) != -1) {
out.write(c);
}
} catch (e) {
print("Error generating dynamic PDF: " + e.toString());
} finally {
if (fileIn != null) {
fileIn.close();
}
if (out != null) {
out.close();
}
facesContext.responseComplete();
}
}]]></xp:this.beforeRenderResponse></xp:view>
使用比仅一个 int 值更大的缓冲区来复制文件:
var buffer = new byte[10000];
var len;
while ((len = fileIn.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
这会大大加快下载速度。