Solved

File upload using apache commons fileupload problem

Posted on 2006-07-03
8
1,354 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
Technology Partners: 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
servlet concurrency 13 133
Books that can get me started on JAVA 2 134
What kind of framework must i use to do that ? 2 48
How  can  i  resolve  HTTP Status 404 -? 8 81
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

752 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