Xpages SSJS - 输出文件对于大文件来说很慢

时间:2015-03-21 16:24:42

标签: xpages lotus-domino java-io ssjs

我正在尝试使用Steve Wissel页面的指令输出我在多米诺骨牌服务器的本地文件系统上的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>

1 个答案:

答案 0 :(得分:2)

使用比一个int值更大的缓冲区来复制文件:

var buffer = new byte[10000];
var len;
while ((len = fileIn.read(buffer)) != -1) {
        out.write(buffer, 0, len);
}

这会加快下载速度。