有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带有来自Java stream的PDF文件的浏览器选项卡图标

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

处理请求的控制器的代码:

@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) 个答案