1

I am uploading an excel file from one jsp page. below is the code.

 <form action="Upload.jsp" enctype="MULTIPART/FORM-DATA" method=post >
       <input type="file" name="filename" />
       <input type="submit" value="Upload" />
  </form> 

But how to get the excel file in the next page(Upload.jsp)? I was using but getting error in the second line.

InputStream file = request.getInputStream();
POIFSFileSystem myFileSystem = new POIFSFileSystem(file );

Then how to get the excel file from the request?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Sweet Dream
  • 255
  • 1
  • 7
  • 14

3 Answers3

2

You are getting a Multipart/form-data on the request from which you need to extract the Part containing your file bytes.

The simplest to do this is to use Apache Commons Fileupload

http://commons.apache.org/fileupload/

Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101
0

Create a FileUploader

import org.apache.commons.fileupload.disk.*;

import org.apache.commons.fileupload.servlet.; import java.io.;

public class FileUploader { private static ServletFileUpload uploader;

private FileUploader()
{

}

public static synchronized ServletFileUpload getservletFileUploader(String tempDir, int maxSizeInMB) 
{
    if(uploader == null)
    {
        DiskFileItemFactory factory = new DiskFileItemFactory();

        factory.setSizeThreshold(1024 * 1024);
        factory.setRepository(new File(tempDir));

        uploader = new ServletFileUpload(factory);

        uploader.setFileSizeMax(maxSizeInMB * 1024 * 1024);
    }

    return uploader;
}

}

Then use it when processing the request

    protected MultiPartFormData handleMultiPartRequest(HttpServletRequest request)
throws FileSizeLimitExceededException
{
    if(!isMultipartRequest(request))
        return null;

    ServletFileUpload upload = FileUploader.getservletFileUploader(tempDir, 50);
    MultiPartFormData data = new MultiPartFormData();
    try
    {
        List<FileItem> items = upload.parseRequest(request);

        for (FileItem item : items) 
        {
            if(item.isFormField())
            {
                data.getParameters().put(item.getFieldName(), item.getString());
            }
            else
            {
                String filename = item.getName();

                //Internet explorer and firefox will send the file name differently
                //Internet explorer will send the entire path to the file name including 
                //the backslash characters etc ... we should strip it down
                //THIS IS HACKY
                if(filename.indexOf("\\") != -1)
                {
                    int index = filename.lastIndexOf("\\");
                    filename = filename.substring(index + 1);
                }


                if(filename == null || filename.equals(""))
                {
                    //do nothing 
                }
                else
                {
                    String randomFileName = (new RandomGUID()).toString() + getFileExtension(filename);


                    File uploadFile = new File(uploadDir + File.separator + randomFileName);
                    item.write(uploadFile);

                }
            }
        }
    }
    catch(FileSizeLimitExceededException e)
    {
        throw e;
    }
    catch(Exception e)
    {
        e.printStackTrace();

    }


    return data;
}

For your reference ... MultiPartForm data looks like

import java.util.Hashtable;

import java.util.ArrayList;

public class MultiPartFormData {

private ArrayList<Integer> fids;
private Hashtable<String, String> parameters;


public MultiPartFormData()
{
    this.fids = new ArrayList<Integer>();
    this.parameters = new Hashtable<String, String>();
}

public ArrayList<Integer> getFids() {
    return fids;
}
public void setFids(ArrayList<Integer> fids) {
    this.fids = fids;
}
public Hashtable<String, String> getParameters() {
    return parameters;
}
public void setParameters(Hashtable<String, String> parameters) {
    this.parameters = parameters;
}

}

jsshah
  • 1,741
  • 1
  • 10
  • 18
  • As to the filename part, read the [FileUpload guide](http://commons.apache.org/fileupload/faq.html#whole-path-from-IE) for a simpler way. – BalusC Mar 15 '12 at 11:57
0

Well guys, thanks for all the reply. But I have resolved the problem with below process.

Inside JSP:

 <form action="/upload.do" enctype="MULTIPART/FORM-DATA" method=post >
      <input type="file" name="file" id="file" size=25/>
      <input type="submit" value="Upload" /> 
    </form>

Created a form bean: inside that

private FormFile file;
    public void setFile(FormFile file) {
        this.file = file;
    }
    public FormFile getFile() {
        return file;
    }

In action class upload:

FileUploadForm uploadForm = (FileUploadForm) form;
FormFile file = uploadForm.getFile();
InputStream stream = file.getInputStream();
POIFSFileSystem fsFileSystem = new POIFSFileSystem(stream);
//
  rest of code for reading the excel
//

Now its working fine.

Sweet Dream
  • 255
  • 1
  • 7
  • 14
  • You should have mentioned from the beginning on that you're using the MVC framework Struts. – BalusC Mar 15 '12 at 11:57