1

Here's what I know:

  • When uploading files given by users, we should put them in a folder outside the deployment folder. Let me call it D:\uploads.
  • We should (somehow) add that folder (D:\uploads) as a web app context.

Here's what I did:

  • I upload my files to the folder D:\uploads.
  • I tried adding the web app context as it's mentionned here by adding the following row to TOMCAT_DIR/conf/server.xml:
    <Context docBase="D:\uploads" path="/uploads"/>

But that doesn't have any effect. When consulting http://localhost:8080/uploads/file.png or http://localhost:8080/uploads I get a HTTP Status 404 error.

So what I want to know:

  1. What did I do wrong ? How can I add my upload folder to Tomcat?
  2. Is there any better approach when it comes to uploading files ? Because I'm wondering what should I change if I want to deploy my application to another server where there's no D:\uploads.
Community
  • 1
  • 1
Auranx
  • 51
  • 1
  • 7

1 Answers1

1

Change the docBase attribute. Use D:/uploads (with slash) instead of D:\uploads (with backslash).

When dealing with files in Java, you can safely use / (slash, not backslash) on all platforms.


Regarding the differences you mentioned in the comments when starting the Tomcat from the IDE and from bin/startup.bat: It's very likely when you start the Tomcat from the IDE, it is not using the same context.xml your Tomcat is using. Just review the Tomcat settings in the IDE.


How to store uploaded files is a common topic at Stack Overflow. Just look around and you'll get surprised in how this topic is popular.

If you aren't happy enough in storing your files in D:/uploads or you'll have other servers accessing the files, you could consider storing them in some location in your network. Depending on your requirements, you can have one dedicated server to store your files or just share the folder which contains the files in your current server. The right decision will always depend on your requirements.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • Thanks for the answer, that did the trick. But I just noticed that when launching the server from the IDE (I'm using Spring Tool Suite) I don't have access to that folder (or any folder inside the webapps). But if I launch the server manually (by executing bin/startup.bat), I can freely access the /uploads path and any folder inside webapps. Is that a normal behaviour or am I missing something ? – Auranx Jan 10 '16 at 00:10
  • I see, I will look for further details. Thanks for the answer, it was really helpful. I'll be glad if you can confirm to me my doubts as I expressed them in the 2nd points in my question (about a better approach). – Auranx Jan 10 '16 at 00:31
  • @Auranx I've just updated my answer. Hope it can help you. – cassiomolin Jan 10 '16 at 00:54
  • 1
    I'm really thankful. The provided answer is very helpful and right to the point. – Auranx Jan 10 '16 at 00:58