aspose file tools*
The moose likes Tomcat and the fly likes Giving path alias to network folder in server.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Giving path alias to network folder in server.xml" Watch "Giving path alias to network folder in server.xml" New topic
Author

Giving path alias to network folder in server.xml

jasdeep parmar
Greenhorn

Joined: Oct 06, 2008
Posts: 20
Hi
I am using tomcat 6.0, I need to read some image files from a network directory.
I have context path setup as
<Context path="/pictures" docBase="\\server\pictures" />
but when i try to read these images using "/pictures/image.jpg" its not coming up on the UI
I tried to access that folder by typing urlpath to that image in IE that also is giving resource not found error..
I am able to access that network drive from the windows explorer directly just not through my app

How can I configure a path to network drive?

thanks


Misha Ver
Ranch Hand

Joined: Mar 03, 2008
Posts: 470
Does it work if you set docBase to your local drive?
jasdeep parmar
Greenhorn

Joined: Oct 06, 2008
Posts: 20
I just tried
<Context path="/pictures" docBase="C:/pictures" />
then
http://localhost:8080/pictures

that also didnt work
Misha Ver
Ranch Hand

Joined: Mar 03, 2008
Posts: 470
jasdeep parmar wrote:that also didnt work


What do you mean by didn't work?

Should be ?

Have you tried to access individual pictures?
jasdeep parmar
Greenhorn

Joined: Oct 06, 2008
Posts: 20
I was getting resource not found error when typing the url below in IE
http://localhost:8080/pictures/image1.jpg


but with backslash
"C:\pictures"
http://localhost:8080/pictures/image1.jpg -- i get the picture


i am able to access local drive but still can't read images on network drive..
I am setting it as below
<Context path="/pictures" docBase="\\server\pictures" />

I am able to access the pictures directly if i type \\server\pictures in windows explorer

thanks


thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18643
    
    8

That is only going to work if the browser and the Tomcat server are on the same physical machine. That's because your URLs point to places on the client machine and not to places on the server.

If you want Tomcat to serve out images which aren't within the web application then the way to do that is via a servlet which gets the image from its actual location and copies it to the response's output. Google keywords: "image servlet".
jasdeep parmar
Greenhorn

Joined: Oct 06, 2008
Posts: 20
This is what i am trying to figure out how to set up the actual location which is on the network server..

thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16134
    
  21

Just to make sure there isn't any confusion, I hope you're aware that "docBase" is the location of the application WAR, and not just "documents"! And from my experience, putting a WAR on a network drive makes it execute unreliably, if at all.

Let me drag out the old mantra:


A web server is not a file server.


As Paul mentioned, the only way to get "files" from outside the WAR is to have a servlet that will open the file localy on the web application server (which CANNOT read files on the client), then have that application copy the contents of the file to the HTTP output stream that the web server is returning to the client. You can't simply direct the client to access the file directly, since

A web server is not a file server.


Avoid using backslashes in file path names in Java. Backslashes come with side effects. Even in Windows, it's safer to say 'docBase="C:/webapps/pictureApp" than to use backslashes.

My other mantra is:

A URL path is not a file path.


URLs are directives to the web server used to route requests to specific webapps. The webapps then dissect the URLs to determine what action to take. The confusion comes from the fact that by default, the action taken is to remove the server and context parts of the URL and use the remaining path part of the URL as a resource path within the docBase.

So for a WAR located in "C:/webapps/dirtypictures" the way to retrieve the image in the "images/hotbabe21/jpg" file if your Context looks like this:



The URL would be "http://servername:8080/familypictures/images/hotbabe21.jpg" and not "http://servername:8080/dirtypictures/images/hotbabe21.jpg"

Because this image is located within the WAR itself, no specialized configuration or copy-to-client logic is required, as the default URL action is being done automatically by the Tomcat server. If your actual images directory was located on a network drive accessible to the web server, a copy-to-client servlet would be required.>


Customer surveys are for companies who didn't pay proper attention to begin with.
jasdeep parmar
Greenhorn

Joined: Oct 06, 2008
Posts: 20
My applications war in not on the network drive.
It is on the local machine where tomcat itself is.
The images i need to get are in "pictures" folder on network drive


To check if the file exist i am using this
String url = webUrl +"/pictures/image1.jpg";
HttpURLConnection co n = (HttpURLConnection) new URL(URLName).openConnection();
con.setRequestMethod("HEAD");
boolean exist = (con.getResponseCode() == HttpURLConnection.HTTP_OK);

LogHelper.log("respose " + con.getResponseCode());
this also return code 404;

so even if I use servlet how do i give it a path as to where to go..

thanks


Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16134
    
  21

That doesn't make sense. What you wrote makes it look like the pictures are coming from another web server, not a network drive (file server).

You sounded more like you wanted something like this:



Where "N" is a network mapped drive. You may need to modify pictureFileName to remove bits of URL that you don't want.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18643
    
    8

jasdeep parmar wrote:My applications war in not on the network drive.
It is on the local machine where tomcat itself is.
The images i need to get are in "pictures" folder on network drive


To check if the file exist i am using this
String url = webUrl +"/pictures/image1.jpg";
HttpURLConnection co n = (HttpURLConnection) new URL(URLName).openConnection();
con.setRequestMethod("HEAD");
boolean exist = (con.getResponseCode() == HttpURLConnection.HTTP_OK);

LogHelper.log("respose " + con.getResponseCode());
this also return code 404;

so even if I use servlet how do i give it a path as to where to go..


You have to have something which maps URLs of the form "/pictures/*.jpg" to a servlet, rather than just allowing it to do its normal thing of returning an image. Then that servlet would look at the URL and find the actual image in the local directory where they are stored, and return ig.
prabal nandi
Greenhorn

Joined: Jun 28, 2011
Posts: 28

i guess Image alias is a feature of Tomcat 7 not tomcat 6.. try doing the same in Tomcat 7.. it works for me
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Giving path alias to network folder in server.xml