Jakarta ee 在web应用程序中显示外部图像

Jakarta ee 在web应用程序中显示外部图像,jakarta-ee,fileinputstream,Jakarta Ee,Fileinputstream,我们的应用程序显示一个外部图像(客户端徽标),该图像特定于多租户环境中的每个客户端。图像文件的位置在运行时动态确定,图像流(以字节为单位)在servlet中传输到浏览器的输出流。它是这样做的 private void sendFileToDownload(HttpServletResponse resp) throws IOException { InputStream inputStream = new BufferedInputStream(new FileInputStream(f

我们的应用程序显示一个外部图像(客户端徽标),该图像特定于多租户环境中的每个客户端。图像文件的位置在运行时动态确定,图像流(以字节为单位)在servlet中传输到浏览器的输出流。它是这样做的

private void sendFileToDownload(HttpServletResponse resp) throws IOException {
    InputStream inputStream = new BufferedInputStream(new FileInputStream(fileNameWitPath));
    OutputStream outputStream = resp.getOutputStream();
    resp.setContentLength((int) inputStream.getLength());
    try {
        writeFileToOutput(inputStream, outputStream);
    } finally {
        finalize(inputStream, outputStream);
    }
}

private void writeFileToOutput(InputStream inputStream, OutputStream outputStream) throws IOException {
    byte[] b = new byte[32 * 1024];
    int count;
    while ((count = inputStream.read(b)) != -1) {
        outputStream.write(b, 0, count);
    }
}

private void finalize(InputStream inputStream, OutputStream outputStream) throws IOException {
    try {
        outputStream.flush();
    } catch (IOException e) {
        logger.debug("IOException was caught while flushing the output stream.", e);
    }
    inputStream.close();
}
在负载测试中,我们发现,当多个线程(~300)执行相同的操作时,打开和关闭文件是代价高昂的操作。特别是FileInputStream构造函数的执行

我想了解在JavaEEWeb应用程序中显示外部图像的替代方案


注意:由于安全原因,我们无法在img html标记的src属性中给出图像的路径。客户端浏览器将无法访问外部图像位置。只有应用服务器才能访问它。

应用服务器前面是否有某种反向代理?如果是这样的话,这可能会为图像缓存提供一些可能性,从而减轻应用服务器的负担。你的意思是说,除了上述代码之外,没有其他更好的选择了吗?我知道反向代理是一种替代方案,但目前我们在应用服务器前面没有web服务器。此外,反向代理还有其他问题。我们如何向反向代理提供映像位置(在运行时由服务器端代码决定),并且代理是否知道对文件所做的任何更改?等