Solved

File upload using apache commons fileupload problem

Posted on 2006-07-03
8
1,332 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Images won't display in JSP pages 30 179
xjc and jaxb 2 90
Servlet Login session time and authentication using Filter 6 68
Best practices to work on a spring MVC project 1 117
There are many Password Managers (PM) out there to choose from. PM's can help with your password habits and routines, but they should not be a crutch you rely on too heavily. I also have an article for company/enterprise PM's.
We need a new way to communicate time sensitive or critical info.   The best part of my role at xMatters is visiting our clients all over the world to learn about how they operate their businesses, share insights that xMatters has gleaned across…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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