Solved

File upload using apache commons fileupload problem

Posted on 2006-07-03
8
1,334 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java cyclic graphs 15 208
String format issue 13 91
EJB MDB example 4 83
I get error: useBean: Duplicate bean name: {0} 1 135
Employees depend heavily on their PCs, and new threats like ransomware make it even more critical to protect their important data.
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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