Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A <code>Servlet</code> or a resource handler can't just serve files from the file system directly. You need to write your own <code>@Controller</code> handler methods. For example</p> <pre><code>@Controller public class ImagesController { public static final String BASE_PATH = "/opt/uploads/contactImages"; @RequestMapping(value = "/{fileName}" , method = RequestMethod.GET) public ResponseEntity&lt;FileSystemResource&gt; getFile(@PathVariable("fileName") String fileName) { FileSystemResource resource = new FileSystemResource(new File(BASE_PATH, fileName)); ResponseEntity&lt;FileSystemResource&gt; responseEntity = new ResponseEntity&lt;&gt;(resource, HttpStatus.OK); return responseEntity; } } </code></pre> <p>With the <a href="http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/http/ResponseEntity.html" rel="nofollow"><code>ResponseEntity</code></a> class you can also set different response headers and status codes.</p> <p>You can now access the above at </p> <pre><code>http://localhost:8080/images/file.png </code></pre> <p>I don't think the above will work for files in nested directories.</p> <hr> <p>Note that the <code>docBase</code> attribute in </p> <pre><code>&lt;Context docBase="/opt/uploads/contactImages/" path="/images" /&gt; </code></pre> <p>is incorrect. The <a href="http://tomcat.apache.org/tomcat-7.0-doc/config/context.html" rel="nofollow"><code>docBase</code></a> attribute specifies</p> <blockquote> <p>The Document Base (also known as the Context Root) directory for this web application, or the pathname to the web application archive file (if this web application is being executed directly from the WAR file). You may specify an absolute pathname for this directory or WAR file, or a pathname that is relative to the appBase directory of the owning Host.</p> </blockquote> <p>So it has to point to your web application, not a random directory where you store files.</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