0

i want to know how to create a folder in webserver(tomcat 7.0) in java.

iam recently start one project.In that project i need to upload files into server from the client machine.In this each client has his own folder in server and upload the files into them.

And in each user folder we have more than two jsp files.when user request the server to show their content by an url (eg:ipaddress:portnumber/userid/index.jsp) using that files i want to show his uploaded data.

is it possible.?

please,guide me to solve this problem. thanks.

3 Answers 3

1

As to your concrete question, just the same way as in a normal Java application.

File root = new File("/path/to/all/uploads");
File newfolder = new File(root, "/userid");
newfolder.mkdir();
// ...

As to your idea with those copypasted JSP files over all folders, don't do that. Just have a single servlet which is mapped on for example /files/* and reads the folder specific to the currently logged-in user and finally forwards to the JSP to present the results. Or if your intent is really to make the uploads public to everyone so that each user can see each other's uploads, then supply the desired user ID as parameter or pathinfo in the request URL like so http://localhost:8080/context/files/userid.

Please note that you shouldn't store the files in the expanded WAR folder, or they will get lost everytime you redeploy the webapp. Store them on a fixed path outside Tomcat's /webapps folder.

2
  • thankyou,for your answer.it's help for me. I have a another doubt.suppose if i have a one main server and more than one sub servers which are connected to mainserver.if suppose user acess my application in mainserver and he wants to upload the data into a the sub server. Is it possible to create a new folder in sub server from the main server.Here the ipaddresses of the subserver is known.
    – mani
    May 15, 2012 at 5:06
  • If it's mapped as a network drive on "mainserver", then you can just keep using File. If not, then you need to consult the responsible serveradmin how to access its disk file system.
    – BalusC
    May 15, 2012 at 5:10
1

You access files and folders from a web application just like any other Java application: using java.io.File or maybe JDK7's new File I/O mechanism. See also the Java I/O Tutorial and the File-related utilities of Apache Commons IO.

1

Ok, here we go.

try {
  File f = new File("file/path/name/.ext");
  if(!f.isDirectory()) {
     boolean success = (new File(f)).mkdirs();
  }
  if(success) {
     System.out.println("Success")
  }
} catch(Exception e) {}

That's it. I hope that functionally. Ciao

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.