File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes saving a file to disk Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "saving a file to disk" Watch "saving a file to disk" New topic

saving a file to disk

Ernesto Elias-Nieland
Ranch Hand

Joined: Sep 01, 2005
Posts: 53

Hi! I try to upload a file by a servlet and want to save it afterwards to disk. I'm using the org.apache.commons.fileupload package to get the file via a http post request. After that i want to create a new object but get an IOException that says something like: " C:\Programme\Apache Group\Tomcat 5.0\webapps\ROOT (access denied)"
The Tomcat is running on a windows xp system and i'm administrator on that machine. The folder in which i want to open the file is not write protected. Perhaps someone knows what i am doing wrong?
Here is my code:

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

DiskFileUpload fu = new DiskFileUpload();
PrintWriter out = response.getWriter();


List fileItems = null;
try {
fileItems = fu.parseRequest(request);
out.print("Opening was succsessful!");
} catch (Exception ex) {
out.print("Opening was not succsessful!");

Iterator iter = fileItems.iterator();
FileItem item = (FileItem);

File uploadedFile = new File(
"C:/Programme/Apache Group/Tomcat 5.0/webapps/ROOT");

try {
out.print("Saving was succsessful!");
} catch (Exception ex) {
out.print("Saving was not succsessful! \n");

0x2B | ~0x2B
SCJD, SCJP 1.5, SCJP 1.4, SCBCD 1.3, SCWCD 1.4, SCJA, IBM Test 150, 142, 700, 701, Oracle Test 1Z0-51
Elihu Smails
Ranch Hand

Joined: Jan 12, 2005
Posts: 37
It might be due to the space in the pathname. I would not place spaces in pathnames or filenames if you want reliable functionality.
I agree. Here's the link:
subject: saving a file to disk
jQuery in Action, 3rd edition