Java: file upload with apache fileupload package - want to read parameters in two separate places

Hello!

I am uploading a file on a JSP page and then handling the request with a servlet and a file processing class in java.

In the servlet I need to read some of the parameters of the request to determine the next JSP screen the user will see and then I call another class/method to handle the file processing. I have been able to read the parameters in the servlet but then there are no parameters left in the other class.

How can I read the request parameters in the servlet and still have access to the file in the next class? Am I going to have to pass the InputStream to the file handler? (Because I would prefer not to do that.)

Let me show you what I am doing:

---servlet---
import org.apache.commons.fileupload.*;

DiskFileUpload  upload = new DiskFileUpload ();
List items = upload.parseRequest (request);
InputStream uploadedStream = null;

Iterator iter = items.iterator();
int i = 0;
String [][] params = new String [12][2];
while (iter.hasNext()) {
      FileItem item = (FileItem) iter.next();
      if (item.isFormField()) {
             System.out.println ("it is a form field");
             params [i][0] = item.getFieldName ();
             params [i][1] = item.getString();
             i++;
      }
      else {
            System.out.println ("this is the file");
            uploadedStream = item.getInputStream();
      }
} //end of while
if (params[1][0].equalsIgnoreCase ("Navigation") {
      next_screen = "app/Home.jsp";
}
else {
      next_screen = "app/FileUploadResult.jsp";
}

---file handler---
import org.apache.commons.fileupload.*;

HttpSession session = request.getSession();
DiskFileUpload  upload = new DiskFileUpload ();
try {
      List items = upload.parseRequest (request);
            
      Iterator iter = items.iterator();
      while (iter.hasNext()) {   //***it never goes inside the while loop***
            FileItem item = (FileItem) iter.next();
            if (item.isFormField()) {
                   System.out.println ("it is a form field");
                  // System.out.println (item.getFieldName());
            }
            else {
                  // System.out.println ("it is a file");
                  String fieldName = item.getFieldName ();
                  filename = item.getName ();
                  //handle file
            }
      } //end of while
} //end of try
catch () {}

If you have any questions for me, just let me know. I'll be watching for responses.
Thanks for your help!
-C
francocaroAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sudhakar_koundinyaCommented:

import org.apache.commons.fileupload.*;

DiskFileUpload  upload = new DiskFileUpload ();
List items = upload.parseRequest (request);
InputStream uploadedStream = null;

Iterator iter = items.iterator();
int i = 0;
String [][] params = new String [12][2];
while (iter.hasNext()) {
     FileItem item = (FileItem) iter.next();
     if (item.isFormField()) {
           System.out.println ("it is a form field");
           params [i][0] = item.getFieldName ();
           params [i][1] = item.getString();
           i++;
     }
     else {
          System.out.println ("this is the file");
          uploadedStream = item.getInputStream();
            FileHandler handler=new FileHandler(uploadedStream );
           handler.save();
     }
} //end of while
if (params[1][0].equalsIgnoreCase ("Navigation") {
     next_screen = "app/Home.jsp";
}
else {
     next_screen = "app/FileUploadResult.jsp";
}


these are lines I have introduced in your Servlet where FileHandler is your fine handler class and save()  is to save the file in disk

            FileHandler handler=new FileHandler(uploadedStream );
           handler.save();
0
sudhakar_koundinyaCommented:
So you need to pass an InputStream object to your file handler class and process that InputSteram in save method
0
petmagdyCommented:
offcourse if u were using Jakarta Struts, it is much simpler as Struts automatically handle this multipart request and return the form fields in the form bean
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

sudhakar_koundinyaCommented:
mport org.apache.commons.fileupload.*;

DiskFileUpload  upload = new DiskFileUpload ();
List items = upload.parseRequest (request);
InputStream uploadedStream = null;

Iterator iter = items.iterator();
int i = 0;
String [][] params = new String [12][2];
while (iter.hasNext()) {
     FileItem item = (FileItem) iter.next();
     if (item.isFormField()) {
           System.out.println ("it is a form field");
           params [i][0] = item.getFieldName ();
           params [i][1] = item.getString();
           i++;
     }
     else {
          System.out.println ("this is the file");
          uploadedStream = item.getInputStream();
            FileHandler handler=new FileHandler(uploadedStream,item.getName() );
           handler.save();
     }
} //end of while
if (params[1][0].equalsIgnoreCase ("Navigation") {
     next_screen = "app/Home.jsp";
}
else {
     next_screen = "app/FileUploadResult.jsp";
}


class FileHandler
{
    FileHandler(InputStream in, String name)
    {
               this.in=in;
               this.name=name;
     }

void save()
{
          FileOutputStream fout=new FileOutputStream()
              ;
             while(true)
              {
                      int n=in.available();
                       if(n<=0) break;
                     byte array=new byte[n];
                        int readBytes=  in.read(byte);
                        fout.write(byte,0,readBytes);

              }
            fout.close();
            in.close();
        }

InputStream in;
String name;name

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sudhakar_koundinyaCommented:
>> FileOutputStream fout=new FileOutputStream()
 FileOutputStream fout=new FileOutputStream(name);
0
francocaroAuthor Commented:
Thank you sudhakar_koundinya!
That's what I needed! Your multiple answers were a little confusing but I played with the code a little bit and got it to do exactly what I needed. THANK YOU!

petmagdy: Thanks for the tip but the apache fileupload package is the only one approved for use within my company.

-C
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.