Solved

File upload using apache commons fileupload problem

Posted on 2006-07-03
8
1,322 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
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
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

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

20 Experts available now in Live!

Get 1:1 Help Now