У меня есть веб-приложение, которое выводит PDF в браузер.Ожидаемое поведение - когда я нажимаю кнопку, происходит предварительный просмотр PDF, а затем, когда я нажимаю кнопку загрузки, - загружается.
Кнопка загрузки не работает - при нажатии на нее просто начинается загрузка пустого файла изавершается с сообщением «Failed network connection».
С другой стороны, когда я хочу «напечатать» PDF-файл, он также показывает кнопку «Сохранить», и эта кнопка работает:
Я попытался изменить заголовки, настройки серверов,Я чувствую, что попробовал все ... любая идея, в чем может быть причина?
Странная вещь - это работает в Firefox, а также в Internet Explorer
Мои заголовки установлены так:
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Disposition: inline; filename="file.pdf"
Content-Type: application/pdf
Date: Wed, 20 Feb 2019 12:38:27 GMT
Expires: 0
Pragma: no-cache
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
КОД JAVA, который генерирует pdf
String ctype = "pdf".equals(reportType) ? "application/pdf" : "text/html; charset=utf-8";
String ext = "pdf".equals(reportType) ? "pdf" : "html";
InputStream srcInput;
try {
String url = buildPath(....);
srcInput = new URL(url).openStream();
} catch (IOException ex) {
ext = "txt";
ctype = "text/plain; charset=utf-8";
srcInput = new ByteArrayInputStream(("Load error: " + ex.getMessage()).getBytes());
}
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
// Open file.
input = new BufferedInputStream(srcInput);
// Init servlet response.
response.reset();
response.setHeader("Content-Type", ctype);
//response.setHeader("Content-Length", String.valueOf(file.length()));
String filename = "file.pdf";
response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
// Write file contents to response.
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
// Finalize task.
output.flush();