3

我正在尝试使用 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>
4

1 回答 1

3

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

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

这将大大加快下载速度。

于 2015-03-21T18:42:40.647 回答