[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-11-04
6
Medium Priority
?
409 Views
Last Modified: 2013-11-24
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
0
Comment
Question by:francocaro
  • 4
6 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12493915

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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12493928
So you need to pass an InputStream object to your file handler class and process that InputSteram in save method
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12493971
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 1500 total points
ID: 12493985
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12494193
>> FileOutputStream fout=new FileOutputStream()
 FileOutputStream fout=new FileOutputStream(name);
0
 

Author Comment

by:francocaro
ID: 12494426
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

867 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