Servlets 显示图像JSF

Servlets 显示图像JSF,servlets,jsf-2,Servlets,Jsf 2,我试图显示存储在本地磁盘上的映像,但未成功 在我的index.xhtml中,我有: <h:graphicImage id="image" value="#{tableData.imageTitle}" /> 我的豆子看起来像这样: @ManagedBean @SessionScoped public class TableData { private String imageTitle = "Image_ON.png"; public String ge

我试图显示存储在本地磁盘上的映像,但未成功

在我的index.xhtml中,我有:

<h:graphicImage id="image" value="#{tableData.imageTitle}" />
我的豆子看起来像这样:

@ManagedBean
@SessionScoped
public class TableData {

    private String imageTitle = "Image_ON.png";

        public String getImageTitle(){
        return imageTitle;
    }
}

我不明白为什么找不到该图像。

我认为您应该尝试使用。它非常简单,无需创建任何类型的servlet等。

您已经将servlet映射到后缀模式
*.png
,而不是像您自己在教程中发现的那样映射到前缀模式
/image/*
。这样,将始终返回
null
。您需要这样做。

我认为最好的解决方案是将apache放到服务器静态文件中。但是把你的代码贴出来。
/*
 * The Image servlet for serving from absolute path.
 */
public class ImageServlet extends HttpServlet {

    // Constants
    // ----------------------------------------------------------------------------------

    private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.

    // Properties
    // ---------------------------------------------------------------------------------

    private String imagePath;

    // Actions
    // ------------------------------------------------------------------------------------

    public void init() throws ServletException {

        // Define base path somehow. You can define it as init-param of the
        // servlet.
        this.imagePath = "C:\\Documents and Settings\\user\\images";
        //this.imagePath = PlatformConfig.getProperty("LocalOperation.Images.Path", null);
        System.out.println("imagePath = " + imagePath);

        // In a Windows environment with the Applicationserver running on the
        // c: volume, the above path is exactly the same as "c:\images".
        // In UNIX, it is just straightforward "/images".
        // If you have stored files in the WebContent of a WAR, for example in
        // the
        // "/WEB-INF/images" folder, then you can retrieve the absolute path by:
        // this.imagePath = getServletContext().getRealPath("/WEB-INF/images");
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // Get requested image by path info.
        String requestedImage = request.getPathInfo();
        System.out.println("requestedImage = " + requestedImage);

        // Check if file name is actually supplied to the request URI.
        if (requestedImage == null) {
            // Do your thing if the image is not supplied to the request URI.
            // Throw an exception, or send 404, or show default/warning image,
            // or just ignore it.
            response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
            return;
        }

        // Decode the file name (might contain spaces and on) and prepare file
        // object.
        System.out.println(imagePath);
        File image = new File(imagePath, URLDecoder.decode(requestedImage,
                "UTF-8"));

        // Check if file actually exists in filesystem.
        if (!image.exists()) {
            // Do your thing if the file appears to be non-existing.
            // Throw an exception, or send 404, or show default/warning image,
            // or just ignore it.
            response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
            return;
        }

        // Get content type by filename.
        String contentType = getServletContext().getMimeType(image.getName());

        // Check if file is actually an image (avoid download of other files by
        // hackers!).
        // For all content types, see:
        // http://www.w3schools.com/media/media_mimeref.asp
        if (contentType == null || !contentType.startsWith("image")) {
            // Do your thing if the file appears not being a real image.
            // Throw an exception, or send 404, or show default/warning image,
            // or just ignore it.
            response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
            return;
        }

        // Init servlet response.
        response.reset();
        response.setBufferSize(DEFAULT_BUFFER_SIZE);
        response.setContentType(contentType);
        response.setHeader("Content-Length", String.valueOf(image.length()));
        response.setHeader("Content-Disposition",
                "inline; filename=\"" + image.getName() + "\"");

        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        response.setDateHeader("Expires", 0); // Proxies.

        // Prepare streams.
        BufferedInputStream input = null;
        BufferedOutputStream output = null;

        try {
            // Open streams.
            input = new BufferedInputStream(new FileInputStream(image),
                    DEFAULT_BUFFER_SIZE);
            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);
            }
        } finally {
            // Gently close streams.
            close(output);
            close(input);
        }
    }

    // Helpers (can be refactored to public utility class)
    // ----------------------------------------

    private static void close(Closeable resource) {
        if (resource != null) {
            try {
                resource.close();
            } catch (IOException e) {
                // Do your thing with the exception. Print it, log it or mail
                // it.
                e.printStackTrace();
            }
        }
    }
}
@ManagedBean
@SessionScoped
public class TableData {

    private String imageTitle = "Image_ON.png";

        public String getImageTitle(){
        return imageTitle;
    }
}