Solved

mkdirs()

Posted on 2008-10-16
8
291 Views
Last Modified: 2013-11-24
Hi

I need to create a directory under webapps/test/
which test is the application folder
http://localhost:8080/test/

I want to create http://localhost:8080/test/upload/abc/

The code does not create any folder.
private String uploadPath = "/upload/abc/", filepath;

filepath = request.getContextPath() + uploadPath;

File tempPathFile = new File(filepath);

if (!tempPathFile.exists()) {

tempPathFile.mkdirs();

}

Open in new window

0
Comment
Question by:techques
  • 4
  • 2
  • 2
8 Comments
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
Try to use
private String uploadPath = "upload/abc"; //no / at the end or the beginning
File tempPathFile = new File(request.getContextPath(), uploadPath); //leave Java to do the concatenations
this will at least make sure the folders are created ok.

Also - had you printed request.getContextPath() to make sure it is where it is supposed to be?

Other from this:
- See the log files for permission issues (you might not be able to create folders especially on a Unix if you do not have the permissions. (Put a try catch block - the method should throw an exception if this is the issue)

By the way...
private String uploadPath = "/upload/abc/", filepath;
is a bad practice and can lead to some strange issues if you need to change something... use
private String uploadPath = "/upload/abc/";
private String filepath;
0
 

Author Comment

by:techques
Comment Utility
request.getContextPath()

/upload/abc/

if i use getServletContext().getRealPath(uploadPath);

C:\\Tomcat 6.0\\webapps\\test\\upload\\abc
0
 

Author Comment

by:techques
Comment Utility
still cannot create directory under tomcat webapps

i think it cannot do that, may have to use absolute local path
0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
That's it I think. You need to pass the FULL path (that's why I asked for the request.getContextPath() -- if it is relative, you should get the real path.
In any way - I am always getting to real paths for any directory creations.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:techques
Comment Utility
I think servlet cannot do it as I finally need to use the local full path
C:\\Tomcat 6.0\\webapps\\test\\upload\\abc

Neither request.getContextPath nor getServletContext().getRealPath did not work
0
 
LVL 27

Expert Comment

by:rrz
Comment Utility
0
 

Author Comment

by:techques
Comment Utility
give up, i think servlet does not support it
0
 
LVL 27

Accepted Solution

by:
rrz earned 50 total points
Comment Utility
Don't give up. Post your complete code. We will debug it
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Fix45 challenge 15 83
count7 challenge 12 66
changePi Challenge 15 75
count11 challenge 6 47
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

763 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now