Accessing Linux Local File System from Java Web Application

Accessing linux local file system from java web application

To quickly answer your question: You can access the file system from a web application, but you would have to check your application server / web container on how to configure the SecurityManager (if one is installed).

However, your method of reading the file has severe issues which you should adress:

  1. Do not check if(!file.exists()) better check if(!file.isFile()). The first check also returns true if the file is a directory.

  2. If there is not a file, better not return a String, throw an Exception, or load some default Configuration or do something else which is useful.

  3. Your logic for reading the file is very bad. If the read function returns a different amount of available bytes (maybe the read is chunked), you'll get a corrupt result.

  4. You do not close the stream in a finally block, (or use a try with resources if you are using Java 7).

  5. The exception handling is also not good. Printing the stacktrace is not good, logging it would be better. Handling the exception would be best (throw an exception, or switch to some default configuration, like when the file was not there).

Edit: If you want to access the client's file system, then this cannot be done from your web application running on the server directly. This of course would have to be done from code running on the client and you would have to fill in more details on what is running on the client side, since a "standard" web application would have Javascript and (X)HTML on the client, not java.

How to access local path from linux server java web application?

You can share that folder on windows and mount that shared folder in unix. Once mounted, it can be easily accessed using samba(smb://192.168.1.117/Your_Folder).

Samba is standard on nearly all distributions of Linux and is commonly included as a basic system service on other Unix-based operating systems as well.

Accessing local files on Tomcat

You're probably better off looking at Tomcat Aliases. They let you mount external directories in to your web app's context, and have Tomcat serve the file directly.

Addenda:

The part of a URL before the : is called the "scheme". Examples include "http", "https", "ftp", as well as "file".

The scheme tells the browser "what to do" with the rest of the URL, different schemes can have different formats after the :.

So, a URL starting with file:// use the "file" scheme. The file scheme tells the browser to try and open the filename specified by the rest of the URL. That filename is a LOCAL filename. Local to the browser. Not the server.

You would almost never use the file:// type of URL on a server based application, but only on a client side application.

Basics - reading/writing remote files using Java

If you want to access a filesystem on a remote computer, then this computer has to make his filesystem available with a service. Such a service is typically a background job, which handles incoming requests and returns a response, e.g. for authentication, authorization, reading and writing. The specification of the request/response pattern is called a protocol. Well known protocols are SMB (or SAMBA) on Windows or NFS on UNIX/LINUX. To access such a remote service you mount the remote filesystem on the level of the operating system and make it available locally as a drive on Windows or as mount point on UNIX.

Then you can access the remote file system from your Java program like any local file system.

Of course it is also possible to write your own file service provider (with your own protocol layer) and run it on the remote machine. As transport layer for such an endeavor sockets (TCP/IP) can be used. Another good transport layer would be the http protocol, e.g. with a restful service or something based on WebDav.

Accessing files/directories in webapp folder in Spring

How exactly did you deploy your application?

ServletContext().getRealPath("/") may return null if it is not deployed as exploded. Read the link below for further information. However, the method to configure this may not be the same for your servlet container.

http://ananthkannan.blogspot.com/2009/12/servletcontextgetrealpath-returns-null.html

Updates

Can someone tell me what's the difference in deploying the WAR as
exploded?

When you deploy the war file as exploded, the servlet container, e.g. Tomcat, will extract the content of war file into a temporary folder and runs everything from that folder so the {WEB_ROOT]/_images/avatars/[myid].jpg is actually exist on file system (hard disk). Therefore, you can actually get the real path (as it already says in the name of the method). However, if your servlet container does not extract the war file, the folder you are looking for is inside the war file and there is no real path to it so it will return null.

Is it something not recommended to do on a production server?
Advantages/disadvantages?

You should not store dynamic contents under your source folder or the webroot folder (webapp) since servlet container will use it temporarily and delete it or change to a new folder when you redeploy your web application. You will likely lost the dynamic data you put into these folders. The web root folder is usually designed for storing static content, which means content you don't want to change - for example, graphic images for your web component like background images, css, etc.

The usual method for storing user data is to somehow create a folder in userspace and put your dynamic data in there. However, you will not be able to serve the content of the folder outside webroot. You will need to write your own static servlet to pipe the data when they are requested. This is quite complicated for a beginner.

The easiest way for implementing your own static servlet to serve dynamic content is to extend the static servlet of your servlet container. However, your code will highly depend on the servlet container you are deploying to.

Since you are going to provide a REST interface for resizing images, you can create a controller which reads in the original images from the dynamic content folder, do the resizing, save it as a temporary file or flush the content of the HttpResponse.

Pass a local file in to URL in Java


new File(path).toURI().toURL();


Related Topics



Leave a reply



Submit