Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File upload using apache commons fileupload problem

Posted on 2006-07-03
8
Medium Priority
?
1,366 Views
Last Modified: 2012-05-05
hi all experts,

  I'm trying to do file uploading using jsp/servlet with apache-commons-fileupload package. There is an example here in,
http://forum.java.sun.com/thread.jspa?forumID=33&threadID=616647

which i follow exactly.

but i cant seems to make it work properly.
it just stop executing at this line,
List items = upload.parseRequest(req);
with out any exception throwed out. Meaning I tried try-catch statement to catch any exception but there seem to be nothing.
What I did is do a System.out.println() on everyline of the code and it stops at this line.
System.out.println doesnt seem to run after this line..

please help. It making me mad.
0
Comment
Question by:sunmaster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 17035101
If you are using the code provided in the example as is.. then it should be working fine.. can you put in the code that u are using in your setting for us to look at?
0
 
LVL 11

Expert Comment

by:Isisagate
ID: 17036611
Verify they you have a form tag that looks like this...

<form method="post" action="/uploadFile" enctype="multipart/form-data">

specifically the enctype=   I have been bitten by that in the past
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17039832
Can you post the error stacktrace? it would be better for us to analyze on what's wrong.

David
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:sunmaster
ID: 17040427
david,
there is no stacktrace at all! very strange.

all,
Sorry for the late reply.
but i manage to solve the problem, but i dunno what is the actual root cause of it. Maybe you experts can enlighten me. :)
the commons-fileupload-1.1.1.jar that i download from apache site seems got problem with it.
What i did is to to use the original commons-fileupload-1.0.jar which is in {tomcat.dir}/server/lib instead of commons-fileupload-1.1.1.jar that i downloaded. I'm using tomcat 5.0.28

But i got another problem. I need to upload to a temporary folder 1st. When the user confirms, Then I'll move the file to the final location.
My questions is will window auto delete the tempory file after a while?

thanks in advance.
here is my code excerpt,

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
      System.out.println("[POST METHOD]");
      processRequest(req,resp);
}

protected void processRequest(HttpServletRequest req, HttpServletResponse resp)
throws IOException{
      try{

            FileUpload fup=new FileUpload();
            boolean isMultipart = FileUpload.isMultipartContent(req);
            // Create a new file upload handler
            DiskFileUpload upload = new DiskFileUpload();
            // Parse the request
            List items = upload.parseRequest(req); // give u the list of items in the form
            Iterator iter = items.iterator();
            while (iter.hasNext()) {

                  FileItem item = (FileItem) iter.next();

                  if (!item.isFormField()) {
                        File cfile=new File(item.getName());
                        System.out.println(""+cfile.getName());
                        //File tosave=new File(getServletContext().getRealPath("/"),cfile.getName());
                        File tosave = createTempDir(cfile.getName());
                        //saving file
                        item.write(tosave);
                  }
            }

            msg = "Attachment successfully added.";
      }catch(Exception e){
            msg = "Fail to upload attachment";
            System.out.println(e);
      }finally{
            resp.sendRedirect(resp.encodeRedirectURL("iss/new_apps/fileUpload.jsp?__msg="+msg+"&__fileNm="+tempFile));
      }
}


private File createTempDir(String fileNm){

      String tempdir = SystemProperties.get("temp.dir");
      boolean exists = (new File(tempdir)).exists();
      if(!exists){
            //temp folder not exist,creating folder at tempdir
            (new File(tempdir)).mkdirs();
      }
      File tempDir = null;
      try{
            tempDir = File.createTempFile(getFilePrefix(fileNm),getFileSuffix(fileNm),(new File(tempdir)));
            tempDir.deleteOnExit();
            tempFile = tempDir.getAbsolutePath();
      }catch(IOException io){
            System.out.println("FAIL TO CREATE TEMPORARY DIRECTORY...");
            io.printStackTrace();
      }
      return tempDir;

}

private String getFilePrefix(String fileNm){
      if(fileNm.indexOf(".") != -1){
            return fileNm.substring(0,fileNm.indexOf("."));
      }else{
            return "DEFAULT";
      }
}

private String getFileSuffix(String fileNm){
      if(fileNm.indexOf(".") != -1){
            return fileNm.substring(fileNm.indexOf("."),fileNm.length());
      }else{
            return ".tmp";
      }
}
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17040657
If you specify the deleteOnExit, yes, they will be removed.

David
0
 
LVL 1

Author Comment

by:sunmaster
ID: 17173436
here is my sollution that i found out and poster earlier

"but i manage to solve the problem, but i dunno what is the actual root cause of it. Maybe you experts can enlighten me. :)
the commons-fileupload-1.1.1.jar that i download from apache site seems got problem with it.
What i did is to to use the original commons-fileupload-1.0.jar which is in {tomcat.dir}/server/lib instead of commons-fileupload-1.1.1.jar that i downloaded. I'm using tomcat 5.0.28"

thanks
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 17205930
Closed, 125 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
By default Outlook 2016 displays only one time zone in the Calendar. The following article explains how to display two time zones in one calendar view.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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