how to serve files for download in j2ee webapp - java

I am working on a feature where i have to provide a direct link to download certain files. all my files are in htdocs folder and i want to provide a direct link to download them like
webapp.com/files/file1.avi
could write a servlet and send the user a file.I know how to do it.
The other way is that if i directly go to webapp.com/files i should be able to see all of the files present in the directory. How do i do that?
do i have to make any server config changes to achieve it?
the webapp is running on Apache tomcat and jdk 1.4

You can directly serve your static content from tomcat(if it is your app server) root directory. I assume other app/web servers will have a static content directory as well.

Related

Securing subdirectory in Tomcat

So a bit of background I have a surface level understanding about Tomcat WAR/Java files but I am helping out the programming team as I am taking over managing our Linux servers.
We are utilizing Apache Tomcat 8.5 and we currently have a WAR file that has a database login xml file. We would like to pull out the xml file into its own directory but I want to secure it down to make sure it can only be accessed from the WAR files on the server and not directly by url.
(edit) XML outside of a war file not from within
I'm open to any idea and any form of implementation.

how to access external files that are not in the web app folder using apache server

I have developed a JAVA webstart application using JNLP,am planning access jar files related jnlp from the outside webapp folder how to access those jar files.
jar files related jnlp from the outside webapp folder
Jars in the web app. directory are never meant for client distribution. They stay on the server and are protected.
A Java Web Start app. obviously needs to download all the Jars and related resources to the client, so every one of those resources must be available by URL on a public part of the server. Always make sure you can fetch the resource (Jar, JNLP, icons etc.) using a direct link in your favorite browser. If it does not allow it, neither will it work in JWS apps.

Can't locate the file on local file system from Servlet

I wrote a RESTful web service using Jersey library and in order to respond to the request I have to read a text file in local file system. C:\data.txt. The servlet works fine when I run it with tomcat on my own laptop.
But when I deploy the war on another machine running windows server OS and place the data.txt again at C:\data.txt. The servlet can't locate the file correctly. Anyone has idea about why is this?
Thanks a lot!
Check whether the Tomcat server process has read-access rights to file C:\data.txt. Check which user is used for running the tomcat process and check the corresponding user file permissions.
I also suspect it is a security error. Applications are usually restricted to reading and writing within their own directory under $TOMCAT_HOME/webapps. If this is the case, you need to change $TOMCAT_HOME/conf/catalina.policy to allow you to access other directories.
If you don't need to write to the file, consider moving its location inside of the classpath by putting in into $TOMCAT_HOME/lib instead.
I think this approach is also better in regards to being cross-platform.

IS there any way to add files to a deployed web application on tomcat server

I'm working on a jsf2 web application and i need to upload files to a folder in my webcontent and keep them permanently, I used the technique that is mentioned in JSF FileUpload Directory and i could upload to wtp... folder's subfolder, but as BlueC said it get lost when tomcat restarts, is there any way to do that?
You can store the files in a Database using a BLOB field. It will depends of how you are implementing the persistence layer of your application but in the Java side usually you will implement this field as byte[]. So it will be comfortable for download or display the file.
You can upload the files to a certain directory/ default directory and then manually copy the files to a secured folder that is outside of the deployment directory and application server installation (yes you can do that).

JFilechooser for a remote server

I have a java application that, at some stage, select files from the local filesystem using JFileChooser.showOpenDialog().
Now I want it in a client-server setup. This means opening a GUI(file browser) for selecting files on the server. I have already tried several scenarios to keep using the JFileChooser, but I cant get the correct setup working.
Does anyone know how to construct a file browser on the remote server if i know the path the file browser needs to default to in advance?
So for eg: if the server address is "http://sand.int.group.com:9083" and on this address the directory that i want to default to is at "/home/myDir/". How can I display the file browser window so that it lists the files located in the directory on this server?
JFileChooser can't load a URL. You will need to map a network share on the machine that wants to browse to the files.
This will serve you purpose, I believe: http://vfsjfilechooser.sourceforge.net/index.html
A remote file chooser based on JFileChooser code...
AFAIK this is not working out of the box. The local JFileChooser has no idea about files located on server side.
Take WebSphere for example, with the Websphere admin tool you can deploy local EAR/WAR files to the server, IBM is doing this with a JFileChooser. But when you are going to deploy a file which is on the remote server you get a completely different UI. So it seems to be very tricky.
One possible way might be to implement your own FileSystemView, but I am not sure, this is just an idea.

Categories

Resources