【发布时间】:2014-04-02 05:51:36
【问题描述】:
根据BalusC's homepage,我实现了函数downloadPDF(),它从文件系统中读取一个PDF文件并在浏览器中显示。此功能按预期工作。
此外,我有一个类EncryptionService,它可以让我加密和/或解密给定的文件。这也可以按预期工作。
不幸的是,似乎无法读取 PDF 文件、对其进行解密并在浏览器中显示。它以浏览器尝试一遍又一遍地加载文件而不显示任何内容而结束。
下面的代码显示了我对 BalusC 的 PDF 处理程序的简单修改。
public void showDocument(String path, boolean decrypt) throws IOException, InvalidKeyException {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
File file = new File(path);
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
if(!decrypt)
input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
else {
// update MessageDigets, return Key
cipher.init(Cipher.DECRYPT_MODE, genKey(passphrase));
input = new BufferedInputStream(new CipherInputStream(new FileInputStream(file), cipher), DEFAULT_BUFFER_SIZE);
}
response.reset();
response.setHeader("Content-Type", "application/pdf");
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + path + "\"");
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
} finally {
try {
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
facesContext.responseComplete();
}
【问题讨论】:
-
具体在什么时候发生循环?
showDocument是否完成执行?什么被发送回浏览器?在浏览器的网络选项卡中查看响应代码、标头和响应正文包 -
@kolossus,
showDocument确实完成了。当我在新选项卡中加载某些内容时,网络选项卡不显示任何内容。尝试在同一选项卡中加载时,它会显示 system.jsf(当前站点)。几秒钟后,这会导致错误net::ERR_CONTENT_LENGTH_MISMATCH。 -
这样你就可以了:你设置为
Content-Length!= 现实中通过网络传输的内容。 -
你能告诉我这个值应该是多少吗?如果文件的长度,我真的不明白内容长度应该是多少。
-
一方面,加密可能会增加文件的大小,而解密会逆转效果。话虽如此,您通常不希望对文件的两个变体重用相同的变量:缓存和一般 I/O 不可靠性可能会导致问题。因此,为了进行实验,对 pdf 的加密和解密形式使用单独的
Files;不要为两者重复使用file
标签: java jsf pdf encryption primefaces