• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

Permitting access to a directory

Hi,

I have a j2EE application deployed  on tomcat, part of the functionality allows the user to upload files, the storage area of which is configurable
at deploy time.  

So for example uploaded files can be stored in 'C:\temp\uploaded-files', the uploads work fine and files are stored correctly but how do I get tomcat to allow access to the same directory for a specific user?  

Thanks
Q

0
q2004
Asked:
q2004
  • 4
  • 2
1 Solution
 
bloodredsunCommented:
I'm assuming that the OS is Windows (as *nix would be very different).

You just need to read a file from that directory nad then output it via the ServletOutputStream/PrintWriter from the respose object. Which you use depends on whether the file is a text file or a binary one.

I would also recommend using a relative path from the webapp. you can find out where you are by calling: getServletContext().getRealPath("/");

e.g. in a servlets doPost or doGet using Writer for text file

String filePath = "blah" ;
PrintWriter out = response.getWriter() ;//for binary use ServletOutputStream out = response.getOutputStream();
    try {
        BufferedReader in = new BufferedReader(new FileReader( filePath );
        String str;
        while ((str = in.readLine()) != null) {
            out.print(str);
        }
        in.close();
    } catch (IOException e) {
    }

Don't forget to set the correct content-type (mime-type).

0
 
cincin77Commented:
Do you mean to change the USER-RIGHTS for that folder or file on Windows?
0
 
bloodredsunCommented:
If you use the above method, the user will be your web server (such as Tomcat) so it must be allowed to write to the folder, so yes, the read/write/execute permissions of that folder and files inside that folder.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
q2004Author Commented:

What I'm getting at is how can files stored outside my web applications directory be made accessible? if you link to it directly tomcat won't that
directory be viewable.

if I'm storing files here for example C:\temp\uploaded-files, how can files in there be made accessible via a href link?



0
 
bloodredsunCommented:
>>What I'm getting at is how can files stored outside my web applications directory be made accessible?
>>if you link to it directly tomcat won't that directory be viewable.

You can't link to it directly. Most web servers will block you, and rightly so.

>>if I'm storing files here for example C:\temp\uploaded-files, how can files in there be made accessible via a href link?

By using a FileReader in a servlet located inside the webapp and outputting the response from it (I gave an example earlier).

Say you have a servlet at the address http://www.myhost.com/myapp/myFileReader.do, this servlet can read a file on the local file system and then forward it to the user as if they were reading it directly.

myFileReader.java (servlet code)
.............
public void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filePath = "blah" ;
PrintWriter out = response.getWriter() ;//for binary use ServletOutputStream out = response.getOutputStream();
    try {
        BufferedReader in = new BufferedReader(new FileReader( "C:\\files\\myFile.txt");
        String str;
        while ((str = in.readLine()) != null) {
            out.print(str);
        }
        in.close();
    } catch (IOException e) {
    }
}
..........

The servlet would read the file at c:\files\myFile.txt and output it to the browser. If it's a application file then use ServletOutputStream as I previously said and make sure that you send the correct response content-type for the file.

I know you are new to JSP/EE, but please read previous posts, as I'm saying nothing here that I haven't said already :-)

 

0
 
q2004Author Commented:
Thanks for that, it works fine with ASCII,  I've tried the following with when serving up binary files which isn;t working:


            String filePath = "C:\\files\\logo_sm.gif" ;                        
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("image/gif");
               ....

Any ideas what I'm doing wrong here?




0
 
bloodredsunCommented:
It's not goin to work as binary and ascii are two different categories. You'll have to write them out using streams, i.e. getting the ServletOutputStream from the response object and transfer the bytes from the file rather than Strings.

Go here http://www.cs.wcupa.edu/~rkline/Java/file_access.html and look at how to read/write binary files. The only difference is rather than using a BufferedOutputStream you'll use the ServletOutputStream to write from.



0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now