2

I want to do the following in tomcat 5.5

1. upload a excel file
2. process the file based on some crieteria
3. show the result

I am able to do all from 2 to 3 but not able to upload a file in tomcat 5.5 and could not also find example.

Pleaes help me.

Joe
  • 4,460
  • 19
  • 60
  • 106
  • possible duplicate of [How to upload files in JSP/Servlet?](http://stackoverflow.com/questions/2422468/how-to-upload-files-in-jsp-servlet) – BalusC Jun 20 '12 at 11:39

3 Answers3

2

Maybe you could give a try on Apache commons fileUpload

You can get an sample here

A more hands-on with not so much conceptual and clarification things could be found here.

On your Servlet you will just use something like:


boolean isMultipart = ServletFileUpload.isMultipartContent(request);

if (isMultipart) {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);

    try {
        List items = upload.parseRequest(request);
        Iterator iterator = items.iterator();
        while (iterator.hasNext()) {
            FileItem item = (FileItem) iterator.next();

            if (!item.isFormField()) {
                String fileName = item.getName();

                String root = getServletContext().getRealPath("/");
                File path = new File(root + "/uploads");
                if (!path.exists()) {
                    boolean status = path.mkdirs();
                }

                File uploadedFile = new File(path + "/" + fileName);
                System.out.println(uploadedFile.getAbsolutePath());
                item.write(uploadedFile);
            }
        }
    } catch (FileUploadException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106
  • Storing uploaded files in deploy folder is a bad idea. Also, the given example doesn't support MSIE. – BalusC Jun 20 '12 at 11:40
1

Apache has provided an API for uploading a file. You can try this.

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

chaosguru
  • 1,933
  • 4
  • 30
  • 44
1

Use Apache’s Commons FileUpload and HttpClient.

Here some links to help you out.

Community
  • 1
  • 1
Isuru Madusanka
  • 1,397
  • 4
  • 19
  • 27