0

i want to get dropdown values in my program in order to merge the string to my file path so that the path will change dynamically according to user input.iam new to apache commoms,before i was using o'reilly api.

here is my code :

 @Override
public void doPost(HttpServletRequest request, 
           HttpServletResponse response)
          throws ServletException, java.io.IOException 
{
       //FileItem f1;
   String d1= request.getParameter("sel1");
    String d2=request.getParameter("sel2");
    String d3="/home/adapco/Desktop/output";
    String conc=d3+"/"+d1+"/"+d2+"/";
    filePath=(new StringBuilder()).append(conc).toString();
 //      filePath="/home/adapco/Desktop/output/";
  isMultipart = ServletFileUpload.isMultipartContent(request);
}

i tried to debug and i am getting the wright file path but while proceeding further,the fileItems shows size=0 and and it is not entering the loop because of size0.

    filePath="/home/adapco/Desktop/output/";

if i pass the upload path to the filePath it works fine.

  List fileItems = upload.parseRequest(request);    
  Iterator i = fileItems.iterator();
  while ( i.hasNext () ) 
  {
     FileItem fi = (FileItem)i.next();
     if ( !fi.isFormField () )  
     {         
        String fieldName = fi.getFieldName();
        String fileName = fi.getName();
        String contentType = fi.getContentType();
        boolean isInMemory = fi.isInMemory();
        long sizeInBytes = fi.getSize();
        if( fileName.lastIndexOf("\\") >= 0 ){
           file = new File( filePath + 
           fileName.substring( fileName.lastIndexOf("\\"))) ;
        }else{
           file = new File( filePath + 
           fileName.substring(fileName.lastIndexOf("\\")+1)) ;
        }
        fi.write( file ) ;
        out.println("Uploaded Filename: " + fileName + "<br>"+filePath);

     }

my html :

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
 <title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
 Select a file to upload: <br />
 <form action="upload" method="post"
                    enctype="multipart/form-data">
<input type="file" name="file">
 <br />
<select name="sel1"> 
<option label ="1">aerospace</option>
<option label ="2">automotive</option>
</select>
<select name="sel2">
<option label="1">internal</option>
<option label="2">demo</option>
 </select>
 <input type="submit" value="Upload File" />
 </form>
 </body>
</html>
ksa
  • 311
  • 1
  • 10
  • 29

1 Answers1

1

The request.getParameter() calls should be removed. They are causing that the request body is parsed before Apache Commons FileUpload can parse it. The request.getParameter() should not be used on multipart/form-data requests.

You need to collect normal form fields in the else of your if (!fi.isFormField()).

if (!fi.isFormField()) {
    // Collect uploaded files.
}
else {
    // Collect normal form fields.
}

See also FileUpload User Guide and this answer.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • :in ur answer the .getfieldname and .getname will process the input type.the same will work for dropdown? – ksa May 19 '12 at 10:29
  • There are detailed examples behind the links I posted. – BalusC May 19 '12 at 10:48
  • No problem. It isn't that hard, just move the mouse pointer to the blueish "this answer" text and then press the left button firmly. It's less effort than you would need to enter a comment. – BalusC May 19 '12 at 11:00
  • @baluc i tried and finally worked out(changed the code),its running fine. – ksa May 22 '12 at 06:45
  • @baluc:i have started a new thread,i have posted my partially running program in this link: http://stackoverflow.com/questions/10697317/file-getting-corrupted-after-transfering-using-apache-commons – ksa May 22 '12 at 07:07