Sujen Shah
Sujen Shah

Reputation: 270

Download file to tomcat server from URL

I am working on a project which is built using java servlets and JSP. The system is deployed on a tomcat server. In this, I need to download certain .wav files from a URL. So I wrote a code to download the file. But the problem is that on the local machine I can give the file path as My "C:/", and it successfully downloads, but I dont know what to give as the path when i deploy it on the server. I tried getting the context path and adding that to the filepath but it did not download. I cannot see any errors on the logs also.

Also since .war files are deployed i dont know how to access folders on my server(or even get their proper paths).

I have searched a lot but all solutions show how to download file from URL to local machine but none show how to download file from URL to Tomcat Server.

Please help me out

Upvotes: 0

Views: 2865

Answers (2)

Geert
Geert

Reputation: 3757

  • If files are to be persisted across application deployments, define a directory on the server where the user that runs the servlet container (probably 'tomcat') has write access.

  • You should not download files into the 'context path', because that is considered to be a read-only resource for the web application, containing only java classes and other (compiled) resources. (you probably cannot, because filesystem permissions deny you)

  • If it's for temporary purposes, use the value returned by System.getProperty("java.io.tmpdir") as the base directory for creating temporary files.

  • If you need to construct paths from directory names and file names, do not manually place slashes and backslashes, but use System.getproperty("file.separator")

  • When finding yourself creating lots of low level functions, consider using for example Commons IO instead of reinventing the wheel.

Upvotes: 0

Makis Arvanitis
Makis Arvanitis

Reputation: 1195

Try to create an environment variable in the server, that will hold the root path of you files. In your code access the environment variable using System.getenv and use this as the root of your path.

There is also the possibility to add the files in the war and access them directly (e.g. http://www.example.com/mywavfiles.wav but this will increase the size of your war and normally is not something that you would like as I understood.

Upvotes: 1

Related Questions