Reputation: 43
I am trying to display image placed in some folder at my machine.I have an xhtml page on which i want to display the image.
I have tried using <h:graphicImage value="C:/images/abc.jpg" />
as well as plain HTML tag
<img src="C:/images/abc.jpg" />
but neither of above is working.
Moreover I have tried placing image in WebContent and tried to access it from there and still in vain.where am i going wrong?
I am using Jboss AS 7.0
Upvotes: 4
Views: 6534
Reputation: 651
If you want to access a image outside webContent you need to work a little more. Basically you have three options: you use a servlet, PrimeFaces or OmniFaces. This question was also answered here:
Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag
I had this same problem in the past and I resolved it by using a servlet. I defined it in this way
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
@WebServlet("/images/*")
public class ImageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = request.getPathInfo().substring(1);
File file = new File("C:Images/", filename);
response.setHeader("Content-Type", getServletContext().getMimeType(filename));
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
Files.copy(file.toPath(), response.getOutputStream());
}
}
then, if you want to show C:/images/abc.jpg
you use
<h:graphicImage value="/images/abc.jpg" />
It worked fine for me.
Upvotes: 4