?
Solved

How to Create Folder - Servlet

Posted on 2005-04-13
5
Medium Priority
?
1,549 Views
Last Modified: 2013-11-24
Hello,

I'm trying to upload file and its working. Problem is its upload under context root direcotry. Bellow is the code line...
...
File cfile=new File(item.getName());
File tosave=new File(getServletContext().getRealPath("/"),cfile.getName());
...

I need to know if the directory path not  present, how to create directory folder ??
eg: if I say getRealPath("/xyz") it should create folder xyz and follow the operation of serlvet...


0
Comment
Question by:princehyderabad
  • 2
  • 2
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 13777892
File path = new File(getServletContext().getRealPath("/xyz"));
if (!path.exists())
{
   path.mkdirs();
}
0
 
LVL 16

Expert Comment

by:Joe
ID: 13777997
The mkdirs(); method will also return a boolean.  This will allow you to make sure they actually got created.

if (!path.mkdirs()){
  System.out.println("Error: Failed to create directory structure");
}
0
 

Author Comment

by:princehyderabad
ID: 13778102
one more thing can anyone plz tell me:

In a form I'm passing this hidden value, how should I extract in Servlet.

<form method="post" action="upload" enctype="multipart/form-data">
<input type="hidden" name="dir" value="<% out.println(request.getAttribute("rs"));%>">
</form>

I tried
String a = request.getParameter("dir");
but didnt worked ??
0
 
LVL 92

Expert Comment

by:objects
ID: 13779361
That should work, not sure why it isn't.
0
 

Author Comment

by:princehyderabad
ID: 13785244
WEll the code mkdirs(); is working on windows machine. I tried on linux machine its not. Any idea whats command on Linux machine to make directory..
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
A solution for Fortify Path Manipulation.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question