带有Java流的PDF文件的浏览器选项卡的图标

时间:2016-07-07 11:50:09

标签: java pdf tomcat7 outputstream

在网页上,我有一个指向PDF文件的链接。单击时,我将该文件从配置的外部URL下载到InputStream中,然后将其写入ServletOutputStream。默认浏览器(我认为至少是Chrome和Firefox)之后的行为,它在新标签页中打开该PDF文件。我的问题是,我可以为新标签设置一些图标吗?因为目前它只有一个Tomcat图标。

处理请求的Controller代码:

@RequestMapping(method = GET, value = "/pdfGuide")
public void getPdfGuide(HttpServletResponse response, Principal principal) {

    long start = System.currentTimeMillis();

    String urlString = configKeyValuePropsLPSRepository.findByParamKey(PDF_GUIDE_URL).getParamValue();
    URL url;
    try {
        url = new URL(urlString);
    } catch (MalformedURLException e) {
        LOGGER.error(String.format(URL_MALFORMED, urlString), e);
        return;
    }

    try (InputStream input = url.openStream() /* get PDF file from configured location */) {
        if (input != null) {

            byte[] fileContent = IOUtils.toByteArray(input);

            // set all needed properties and headers for opening the file
            response.setContentType(MediaType.PDF.toString());
            response.addHeader(CONTENT_DISPOSITION, DISPOSITION_INLINE + ETLUtil.getConfigProperty(
                    PDF_GUIDE_FILE_NAME));
            response.setContentLength(fileContent.length);

            // add file to the output stream of the response
            try (ServletOutputStream output = response.getOutputStream()) {
                output.write(fileContent);
                output.flush();
            }

            LOGGER.info(String.format("User [%s] has opened PDF guide. File size is %d KB. Time taken: %d ms.",
                    principal.getName(), fileContent.length / BYTES_IN_KB, System.currentTimeMillis() - start));
        }
    } catch (IOException e) {
        LOGGER.error("Error while trying to download PDF guide", e);
    }
}

0 个答案:

没有答案