Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p><em>I've seen some suggestions like having the image directory being a symbolic link pointing to a directory outside the web container, but will this approach work both on Windows and *nix environments?</em></p> </blockquote> <p>If you adhere the *nix filesystem path rules (i.e. you use exclusively forward slashes as in <code>/path/to/files</code>), then it will work on Windows as well without the need to fiddle around with ugly <code>File.separator</code> string-concatenations. It would however only be scanned on the same working disk as from where this command is been invoked. So if Tomcat is for example installed on <code>C:</code> then the <code>/path/to/files</code> would actually point to <code>C:\path\to\files</code>.</p> <p>If the files are all located outside the webapp, and you want to have Tomcat's <code>DefaultServlet</code> to handle them, then all you basically need to do in Tomcat is to add the following Context element to <code>/conf/server.xml</code> inside <code>&lt;Host&gt;</code> tag:</p> <pre class="lang-xml prettyprint-override"><code>&lt;Context docBase="/path/to/files" path="/files" /&gt; </code></pre> <p>This way they'll be accessible through <code>http://example.com/files/...</code>. GlassFish/Payara configuration example can be found <a href="https://stackoverflow.com/questions/19981640/how-to-display-images-located-in-glass-fish-folder">here</a> and WildFly configuration example can be found <a href="https://stackoverflow.com/questions/34426082/how-can-i-serve-static-resources-from-outside-a-war-on-wildfly">here</a>.</p> <p>If you want to have control over reading/writing files yourself, then you need to create a <code>Servlet</code> for this which basically just gets an <code>InputStream</code> of the file in flavor of for example <code>FileInputStream</code> and writes it to the <code>OutputStream</code> of the <code>HttpServletResponse</code>.</p> <p>On the response, you should set the <code>Content-Type</code> header so that the client knows which application to associate with the provided file. And, you should set the <code>Content-Length</code> header so that the client can calculate the download progress, otherwise it will be unknown. And, you should set the <code>Content-Disposition</code> header to <code>attachment</code> if you want a <em>Save As</em> dialog, otherwise the client will attempt to display it inline. Finally just write the file content to the response output stream.</p> <p>Here's a basic example of such a servlet:</p> <pre><code>@WebServlet("/files/*") public class FileServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filename = URLDecoder.decode(request.getPathInfo().substring(1), "UTF-8"); File file = new File("/path/to/files", filename); response.setHeader("Content-Type", getServletContext().getMimeType(filename)); response.setHeader("Content-Length", String.valueOf(file.length())); response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\""); Files.copy(file.toPath(), response.getOutputStream()); } } </code></pre> <p>When mapped on an <code>url-pattern</code> of for example <code>/files/*</code>, then you can call it by <code>http://example.com/files/image.png</code>. This way you can have more control over the requests than the <code>DefaultServlet</code> does, such as providing a default image (i.e. <code>if (!file.exists()) file = new File("/path/to/files", "404.gif")</code> or so). Also using the <code>request.getPathInfo()</code> is preferred above <code>request.getParameter()</code> because it is more SEO friendly and otherwise IE won't pick the correct filename during <em>Save As</em>.</p> <p>You can reuse the same logic for serving files from database. Simply replace <code>new FileInputStream()</code> by <code>ResultSet#getInputStream()</code>.</p> <p>Hope this helps.</p> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/18664579/recommended-way-to-save-uploaded-files-in-a-servlet-application">Recommended way to save uploaded files in a servlet application</a></li> <li><a href="https://stackoverflow.com/questions/132052/servlet-for-serving-static-content/29991447#29991447">Abstract template for a static resource servlet</a> (supporting HTTP cache)</li> <li><a href="https://stackoverflow.com/questions/2340406/how-to-retrieve-and-display-images-from-a-database-in-a-jsp-page">How to retrieve and display images from a database in a JSP page?</a></li> <li><a href="https://stackoverflow.com/questions/13588149/how-to-stream-audio-video-files-such-as-mp3-mp4-avi-etc-using-a-servlet">How to stream audio/video files such as MP3, MP4, AVI, etc using a Servlet</a></li> </ul>
 

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