Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The images counts each as a <strong>separate request</strong>. You <em>cannot</em> process <em>both</em> the HTML response (of JSF) and the image in a single request/response cycle. You need to store the image/thumb somewhere in a datastore which lives longer than a request, e.g. the server's local disk file system (temp folder? webcontent folder?), or a database (temp table?), or in the session.</p> <p>First, replace</p> <pre><code>&lt;h:graphicImage value="#{fileUpload.thumb}" ... </code></pre> <p>by</p> <pre><code>&lt;h:graphicImage value="thumbs/#{fileUpload.thumbId}" ... </code></pre> <p>so that it get generated as</p> <pre><code>&lt;img src="thumbs/123" ... </code></pre> <p>(the <code>src</code> should namely point to a <strong>valid URL</strong>)</p> <p>Then, create a <code>HttpServlet</code> which is mapped on an <code>url-pattern</code> of <code>/thumbs/*</code> and implement <code>doGet()</code> roughly like follows:</p> <pre><code>Long thumbId = Long.valueOf(request.getPathInfo().substring(1)); // 123 byte[] thumb = getItFromDiskOrDatabaseOrSessionByThumbId(thumbId); String filename = getOriginalFilenameOfUploadedImageOrInventOne(); response.setContentType(getServletContext().getMimeType(filename)); response.setContentLength(thumb.length); response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\""); BufferedInputStream input = null; BufferedOutputStream output = null; try { input = new BufferedInputStream(new ByteArrayInputStream(thumb)); output = new BufferedOutputStream(response.getOutputStream()); byte[] buffer = new byte[8192]; int length; while ((length = input.read(buffer)) &gt; 0) { output.write(buffer, 0, length); } } finally { if (output != null) try { output.close(); } catch (IOException logOrIgnore) {} if (input != null) try { input.close(); } catch (IOException logOrIgnore) {} } </code></pre> <p>That's it. More servlet hints can be found in <a href="http://balusc.blogspot.com/2007/04/imageservlet.html" rel="noreferrer">this article</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload