Link to home
Start Free TrialLog in
Avatar of AntoniRyszard
AntoniRyszard

asked on

Exception using Apache FileUpLoad?

Hello,

I constructed a basic html page to post several files to a java servlet. In the servlet I am using the apache tool fileupload to accept files, and write these to a folder.  

In the code below the servlet loops through the posted files. I noticed the fileupload method (getName()) returns the complete path of the file on the users hard-drive. For example

c:\windows\desktop\tree.jpg

And I am using to write the files to tomcat\files\

When it comes to writing the file an exception is thrown.

Does anyone think I should be parsen the getName() value to recieve just the file name (tree.jpg) and not the full path c:\windows\desktop\tree.jpg.

I hope this makes sense.

Thanks

Iterator iter = items.iterator();

while(iter.hasNext()){
   FileItem item = (FileItem) iter.next();
   File uploadedFile = new File("/tomcat/files/" + item.getName());
   //item.write(uploadedFile);
}
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

>> When it comes to writing the file an exception is thrown

What exception?
Avatar of AntoniRyszard
AntoniRyszard

ASKER

The problem is the exception is displayed in the tomcat java frame. And it's not being written to the tomcat log files.

When I comment out the line item.write(uploadedFile);

There is not exception.

Could it be the write cannot find the tomca/files folder to write to?

I wondered also since the getName() method returns:

c:\windows\desktop\tree.jpg.

I thought this line below would return a file with the path

File uploadedFile = new File("/tomcat/files/" + item.getName());

\tomcat\files\c:\windows\desktop\tree.jpg

Does this seem correct?

Thanks
Does:

    /tomcat/files

exist?

And can the UID that runs tomcat write into it?
The getName() thing (afaik) should be ok :-)
The files folder path is:

c:\tomcat\files

Does this seem correct?
errr...  what?  the file you select with the <input type="file"?

That doesn't matter
In the html I using the input type file.

For the momen I am building the web app on my pc, so tomcat is located at the folder c:\tomcat.

And I created a folder in tomcat to hold the files posted to the servlet.

The servlet will write the files to c:\tomcat\files\
I think you're missing something.  Do you have the <form enctype="multipart/form-data"> set?  What are you using to retrieve the file?  Are you using a MultipartFormDataRequest?  It's unfortunatly not as easy as just copying the filename into the appropriate location.  I can't really tell what's going on but I'm thinking you're not focusing on the right thing.  Post the code where you get the file from the request object.
Thanks

In my html page the form tag is using enctype="multipart/form-data".

I posted below the main parts of the servlet, I am using the apache fileipload tool. The servlet compiles without an error, and only throws an exception when this line is uncommented item.write(uploadedFile). When I comment out this line there's not exception.

At the moment tomcat is installed on my pc at c:\tomcat, so for testing when I write back the files posted to the servlet. I am trying to write the posted files to c:\tomcat\files\

Thanks

public class TestServlet extends HttpServlet{
 
   public void doPost(HttpServletRequest req, HttpServletResponse res)
         throws ServletException, IOException{
      try{
         boolean isMultipart = FileUpload.isMultipartContent(req);
 
         if(isMultipart){
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List items = upload.parseRequest(req);
 
            Iterator iter = items.iterator();
 
            while(iter.hasNext()){
               FileItem item = (FileItem) iter.next();
               File uploadedFile = new File("/tomcat/files/" + item.getName());
               //item.write(uploadedFile);
            }
         }
      }
      catch(Exception e){
         e.printStackTrace();
      }
}
 
}
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That last line would create file in the files folder within your web app root.

Thanks

Would the loop in my servlet be changed to something like this?

   Iterator iter = items.iterator();
 
   while(iter.hasNext()){
      FileItem item = (FileItem) iter.next();
      String filePath = item.getName();      
     
      String FileName = (parsen filePath)

      File uploadedFile = new File(application.getRealPath("/files/") + FileName)
      item.write(uploadedFile);
   }

I was not sure what the (application) variable is?

Thanks
Do you believe there are any good approaches to parsen the String filePath = item.getName(); value.

Thanks
Just put the following line in a JSP.
<%=application.getRealPath("/")%>
application is a reference to a ServletContext instance.  
>Do you believe there are any good approaches to parsen the String filePath = item.getName(); value.
Maybe use String methods to find last file path separator.
if(-1 != filePath.lastIndexOf("\\")
                 FileName = filePath.substring(filePath.lastIndexOf("\\"));   (for windows only)
In the user guide to the fileupload tool.

They only used  item.write() to write there files

http://jakarta.apache.org/commons/fileupload/using.html

Do you think there are better tools available to capture files from the user and store these on the server?
>Do you think there are better tools available to capture files from the user and store these on the server?  
Apache commons fileupload is very popular.
Are you able to upload to temp file ?

     File uploadedFile = new File(application.getRealPath("/files/temp.jpg") )
      item.write(uploadedFile);

 The path to the uploaded file is then
tomcat/webapps/yourContextDirectory/files/temp.jpg
Thanks

I found another example on web which uses the apache fileupload tool. To access the filename for example temp.txt they used in a servlet:

//iter loops through files.

FileItem item = (FileItem) iter.next();
String fileName = item.getName();
fileName = (new File(fileName)).getName();

And this seems to return the temp.txt, and not the path to the file on the users computer.