Link to home
Start Free TrialLog in
Avatar of JavaUser_82
JavaUser_82

asked on

How to access the path of the tomcat webapps when accessiong over the network

How to access the path of the tomcat webapps when accessiong over the network for example

i have a folder usefiles where i have to chek in some files so in my servelt my class if i use
String strFilePath =  getServletContext().getRealPath("/UserFiles");   it works fine onlt when i run the application through localhost and when i try from some other machine which is in network this code is not working it is saying "The system cannot find the path specified" can  you please sayreslove the problem so that anyuser who lohs into my iste can uplaod his files and i can access them

adns orry i assigned 500 points without noticing i am assigning only 225 points for this question
Avatar of jvaj
jvaj

The J2EE spec says that web containers do not have to implement getRealPath . So best is to find another way to do what you need. Think of a clustered environment , where application server will not be able to expose underling file system!

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)

if the war file is not  exploded on deployed server the above method will return null.
Avatar of JavaUser_82

ASKER

Sorry i am very much ne =w to J2ee , actually my requirement is that user uploads some files and i have to store all those files in my tomcat root folder can you plsae suggest a method to do it if it is not possible by getServletContext().getRealPath("/UserFiles");
My advise is to keep the files outside the webapplication root. Otherwise you can lose them when new war files get deployed.

System.getProperty("user.home")

gives you the home directory of the user that runs the web application or

System.getProperty("java.io.tmpdir")
will give you the temp directory of the system.
also if you want them the same files to be available for download in the application, write a servlet to read the files from above location and write to the output stream of the response.
Thanks but  when i keep my site live through hosting providers , this sloution is not feasible as i dont havea access to any of the folder strcuture so thast why i though of keeping in tomcat folder and i have full control on that what do u suggest in this
you will have to have a dbms running as the back end. write the files as blobs to a table.

If you dont have control over the hosting system you must not make assumptions about the file system at all.

If you dont have a dbms running then you can make use of hsqldb embedded in the webapp. just copy hsqldb.jar to the WEB-INF/lib configure the table path to be WEB-INF/data. use jdbc to write/read.

Thanks , But i had though this alterantive approach  of this of storing as bolb data , but i also want to uplaod the music files and video files which i cannot  store as bolb data  thats why i want store as a file system and also if i store in data base the performace  hits so instead i am storing the file path in the data base and and files in the folder


Avatar of rrz
Are you using a war file to deploy your web app ? Does that war file remain unpacked at server ? If yes, then try using something like this JSP.  First you must place the file test.txt in your web app's  WEB-INF/classes  folder.
<%@ page import="java.util.*,java.io.*,java.net.*" %>
<%
    String resource = "test.txt";
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    URL url = loader.getResource(resource);
%>
<%=url.getFile()%>
you can write any information as blob. (binary large object)
  byte[] tmp = new byte[1024];
        byte[] data = null;
        int sz, len = 0;

        while ((sz = fis.read(tmp)) != -1) {
          if (data == null) {
            len = sz;
            data = tmp;
          } else {
            byte[] narr;
            int nlen;

            nlen = len + sz;
            narr = new byte[nlen];
            System.arraycopy(data, 0, narr, 0, len);
            System.arraycopy(tmp, 0, narr, len, sz);
            data = narr;
            len = nlen;
          }
        }
        if (len != data.length) {
          byte[] narr = new byte[len];

          System.arraycopy(data, 0, narr, 0, len);
          data = narr;
        }
        stmt = con.prepareStatement("INSERT INTO BlobTest(fileName, "
            + "blobData) VALUES(?, ?)");
        stmt.setString(1, args[0]);
        stmt.setObject(2, data);
        stmt.executeUpdate();

Open in new window


Anyway If you write to deploy folders of an application server you risk losing that data. Best thing is to speak to the hosting provider of your  requirement ant get disk quota in a seperate location.
With the above code i am unable to stire the information of the Music and video files i can store onlky the  documents and pictures
what is the exception / error your are getting?
Its saying memory out of exception as the file is 4 MB and also i am using mysql data base its taking lot of time for pictures and documents also
in your jvm allocate more heap memory.

To do this where your java command line  is used, add the switch -Xmx512M and restart application server.
Sorry for teh late resposne as i am out of station

ok if i go with your storing file outside the webapplication i am using the System.getProperty("user.home")  and in this i have stored some files but when i am accessing over the network with server  IP address its unable to  access the files on the web

for example if i use System.getProperty("user.home")   it is giving me C:\Document and Settings\User1 and in this folder i copied some images and when i am trayin to access its not displaying for me
as I use String strzFIlepath=System.getProperty("user.home")  +"\\UsrrFiles ( User Files is the folder where all the files are located)   can you say where i am going wrong
>when i am trayin to access its not displaying for me  
Please show us your code.
This is my code and ArrayList alUserMusics = userImageDetails.getAlMusics();  is displaying all the audio files uploaded by the user    




<%
      
      userImageDetails.getUserFileDetails();

      //System.out.println(alUserMusics);

      ArrayList alUserMusics = userImageDetails.getAlMusics();

      System.out.println(alUserMusics);

             String strAPPPath = System.getProperty("user.home")+"\\UserFiles\HELLO\Music";
      
      
%>



The Streaming of Music clips depends on your Internet speed

If streaming is very low you can download and view
 
<!-- CONTENT BEGIN -->




<form name="musicGallery">



      
            
            
">




" NAME="MediaPlayer"
WIDTH="250" HEIGHT="250" ShowControls="1" ShowStatusBar="1" ShowDisplay="1" autostart="0">
            
            
            



</form>



>">  
This won't work. The browser doesn't have access to that path.  
Can you put audio files into root folder of your web app ?  
If not then you  will need to have as Servlet or a JSP to get the content for the browser.
 
where audio.jsp is located in same folder as the JSP you posted.
No i canot keep in the root folder becoz i need to deploy the war file in the hosting server , see my requirement is simple user uploads a file from the browser so i need to store the file  somewhere on the hosting server machine and when he logs in again so i need to show al the audio files uploaded by him so now i cannot acheive this when i keep the war file on the server, so i am palnning to take some space on the hosting machine and when user uploads i store the file in that location and same i retrive it from the same locatiion when he wants to view it , can you please paste psudo code for this that what should i write in the audio.jsp and how should i get the content and i need to pass the arraylist to the page where it displays all the files in the page with a media player
an dcan we achive this by any CGI server side scripts so that user can check in his files and view his files , i am not sure i an just new to j2ee and CGI so seeking your help
i mentioned CGI script for the to access the directories  or folder on the server
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> if(file != null){  
I meant  
 if(fileName != null){
>response.setContentType("application/octet-stream");  
If you know the mime type then use it instead.
For example if you have a  .aif  file then use
response.setContentType("audio/aiff");
yes the above should work.
Yes thanxk  a lot my purpose of half served the other half is that see i should be able to delet the file also becoz user should have option to delete the files uploaded by him and now when i click delet option it is saying the file is used by someone it cannot be deleted, i am not even able to delete manually so unless i shutdown the server i am unable to delete the file or image , so now how do i slove this with this code
The only way I know is using  
myFile.deleteOnExit();    
Otherwise  you could place the files into temp file at  System.getProperty("java.io.tmpdir")
You mean to say as soon as i run the application i should copy the files from the specified folder to the temp folder , and always read form the temp folder , but  there is also a risk of user deleting all the temp folders , and what does deleteONExit do ti deletes the files when the closes the browser can you throw some light on this , and can you say which is the better way
And surely i dont think os we have to use deleteOnExit() . becoz it consumes a lot of memory leak this and allso the system or JVM exit may happen  in a dyas time or in a weeks time for running applicatiuons if each file is  2 MB and this and then  average it will accumulate lot of memory per day , and it may crash , and if i use temp files therr is a risk of deleting all the temp files c do u have any other way to handle this
run an embedded database. store the files as blobs. store the database files in the home folder. I posted you the code earlier.
yeah but if i storte it in a databse the perfromance will be very slow each file uploaded by the user will be on average on 2 MB so wiritng itno data base and again retreving it will take lot of time so thast why insetaed i opted to store the file on the server and display it from the server which wil be comparatively much much fasther than the data base,
And i also got the info from some books and some sites and also form some people that it is always efficient and good to store the file in the filesysten and just store the path in the data base
>and what does deleteONExit do ti deletes the files when the closes the browser can you throw some light on this , and can you say which is the better way  
It has nothing to do with the browser. According to the javadoc, the method
"Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates."    
>And surely i dont think os we have to use deleteOnExit() . becoz it consumes a lot of memory leak  
The file is not held in memory. The deletion action is just put on a list to be done later.    
I think it is your operating system that is preventing the delete.  There might some way around this but I don't know.   What is your server's operating system ?  Maybe someone can provide solution.  
call System.gc() before deleting the file. I should work.