1

I want to create a folder on my server corresponding to every user in my web application.

How i can create a new folder through java dynamically? And i want that new folder to contain resources corresponding to a particular user.Can i now access those resources as a client by directly providing the URL of that resource For ex..

    http://localhost:8080/myapp/newfolder/a.pdf

Is that possible?

3 Answers3

3

I wouldn't expose the folder directly through URLs as that would be a serious security concern where one user can access the folder of another user if the naming scheme is understood.

Instead, I would

  • create folders by user name in a separate location that is not accessible through the URL, using the java.io.File.mkdir().
  • write a simple Servlet that would read the servletRequest.getPathInfo(), identifies the user name from the path, along with the file to be served. For this to happen, the servlet class and URL pattern should be defined as follows:

    <servlet-mapping>
        <servlet-name>read-file</servlet-name>
        <url-pattern>/read-file/*</url-pattern>
    </servlet-mapping>
    

    To access a file a.pdf of user xyz, the URL would be http://server/read-file/xyz/a.pdf.

  • check if the user accessing the URL is same as the user name found in the path info.

  • read the file through a FileInputStream in blocks of, say 1MB, and write into the ServletOutputStream (without storing anywhere in between) after setting the appropriate values in the Content-Type and Content-Disposition headers.
Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • Yeah but many websites on the internet expose the url of the pdf files on them..Will that be a totally wrong approach? – user1911867 Dec 24 '12 at 05:01
  • Can i do it this way.I can use the servlet to identify a user and then forward his request to the url of the resource?? – user1911867 Dec 24 '12 at 05:02
  • 1
    simply forwarding , wont make your resource secure. you need to filter out non authorized access. – Subin Sebastian Dec 24 '12 at 05:08
  • 1
    The URLs on those websites can still read as "/abc/xyz.pdf", but they don't point directly to the physical files on the server. They run through a simple servlet that does some housekeeping work, like number of accesses, lite authentication/authorization check if applicable, etc., – Vikdor Dec 24 '12 at 05:09
  • if a request is for /abc/xyz.pdf..how can a servlet intercept it?? – user1911867 Dec 24 '12 at 05:12
  • Please see the update of how the servlet mapping and url pattern should be. – Vikdor Dec 24 '12 at 05:19
0

Just check this. Create a New folder using Java Program on Windows and Linux machines

further you can store the mapping of path of the directory and corresponding user.

Community
  • 1
  • 1
Harit Vishwakarma
  • 2,338
  • 4
  • 21
  • 36
0

You can create a directory in Java using new File("path"+File.seperator+"foldername").mkdir()

I assume you are aiming to create it within your war folder in web app. This is not a recommended approach, because you will loose user data if you redeploy your war.

Better approach is have a repository path configured into your application using a System property

For eg: repository=D:\userdata And create your folders within this repo.Then you may need a custom servlet to allow download of this files, you will have to read the file and put it in servlet response. This folder wont be affected by redeployment of war file.

Even this approach is wrong as you cannot scale to multiple appservers in this approach, It is always better to store user data in a central repository(amazon s3, rackspace files), or in a database.

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42