Display Images From a Non-Project Directory in JSF
<h:graphicImage value="/headerimage/?img=logo.jpg"/>
Create Servlet :
@WebServlet(name = "DynamicImageServlet", urlPatterns = "/headerimage/*"})
public class DynamicImageServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try {
//Get image file
String img = request.getParameter("img");
BufferedInputStream in = new BufferedInputStream(new FileInputStream("path to absolute location of image/"+img));
//Get image contents
byte[] bytes = new byte[in.available()];
in.read(bytes);
in.close();
//Write image contents to response
response.getOutputStream().write(bytes);
}catch(IOException ex){
Logger.getLogger(DynamicImageServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Comments
Post a Comment