0

I am implementing a file upload for excel i.e xls type document, after selecting the file when I submit upload.parseRequest returns empty list.

List<FileItem> items = (List<FileItem>)upload.parseRequest(request)

Below is the CommonsFileUploadServlet.java for review -

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class CommonsFileUploadServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        File fullFile = null;
        FileItem item = null;
        int FILE_SIZE = 2097152;
        try {
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);

            if (isMultipart) {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                List<FileItem> items = (List<FileItem>) upload.parseRequest(request);
                System.out.println("Number of fields: " + items.size());
                Iterator<FileItem> itr = items.iterator();
                String msg = "";

                while (itr.hasNext()) {
                    try {
                        item = (FileItem) itr.next();

                        if (item.isFormField()) {
                            String fieldName = item.getFieldName();
                            if (fieldName.equals("name")) {
                                request.setAttribute("msg", "Thank You: " + item.getString());
                            }
                        } else {
                            String path = request.getParameter("path");
                            String field = request.getParameter("field");
                            System.out.println("path >>" + path + "<br/><br/>");
                            fullFile = new File(item.getName());
                            String imageName = fullFile.getName();
                            File f = new File(path);
                            f.mkdirs();
                            File savedFile = new File(path + "/", imageName);
                            fullFile = uniqueFile(savedFile, path);
                            item.write(fullFile);
                            msg = "file uploaded successfully";
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        msg = "Problem occured while uploading file.<br>Please try again";
                    }
                }
            } else {
                System.out.println("not a multipart request");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public File uniqueFile(File f,String pa) {

        return f;       
    }

}

The dataMigration.jsp is as below -

<body>
        <center>
            <form action="upload.jsp?path=<%=path%>&field=<%=field%>" name="form" method="POST" enctype="multipart/form-data">
                <b><font face=verdana size=2>Upload File:<br><br></font></b>
                <input type="file" name="uploadFile" id="uploadFile"/><br><br>
                <input type="submit" name="Submit" value="Submit" onclick="return callCheck();"/>
            </form>
        </center>
    </body>

Please find the debugging analysis as below -

  1. In debug mode under variables I can see request variable has files in multi - files - table
  2. But the items variable is empty i/e modcount = 0 & size = 0

web.xml file -

<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>MySystem</display-name>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>com.emsproject</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener>
    <listener>
        <description>sessionListener</description>
        <listener-class>com.emsproject.action.common.SessionListener</listener-class>
    </listener>
    <context-param>
        <param-name>tilesDefinitions</param-name>
        <param-value>/WEB-INF/tiles.xml</param-value>
    </context-param>
    <welcome-file-list>
        <welcome-file>/jsp/common/index.html</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>SysInfo</servlet-name>
        <servlet-class>com.emsproject.action.common.SysInfoServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SysInfo</servlet-name>
        <url-pattern>/eapp/*</url-pattern>
    </servlet-mapping>
</web-app>

I tried many possible alternatives but the parseRequest method returns the empty list

Kindly help me in this regards as I am struggling with this issue the whole day :( I do not want to use s:file i.e struts fileupload feature

Thanks pshinde31

pshinde31
  • 43
  • 8
  • See http://stackoverflow.com/questions/18594770/uploadify-can-not-work-using-servlet/18596465#18596465 – Roman C Sep 08 '13 at 15:19
  • I did check the above post earlier but not sure how to implement the same, also what explicit modifications I need to make in my struts actions so that I can receive the list of files from my request which is present in there. kindly suggest.... – pshinde31 Sep 08 '13 at 15:25
  • Also http://stackoverflow.com/questions/16124366/blueimp-jquery-file-upload-empty-file-upload-result-struts2-fileitems-empty/16124909#16124909 and this one http://stackoverflow.com/questions/16619507/struts-2-file-upload-without-struts-tags/16625712#16625712 – Roman C Sep 08 '13 at 15:29
  • I appreciate your prompt response.I have moved the upload code from jsp to a servlet class & edited the original post.I verified all the above references, but still unable to upload the file. Also as there are no errors logged its making it difficult for me to make the necessary changes. From my above code snippet may I know what changes/modifications are required. My Struts action is added to the original post. Also can I not simply bypass/ignore/avoid Struts 2 fileUpload interceptor from interfering this implementation. – pshinde31 Sep 08 '13 at 17:51
  • I don't understand, why aren't you using the default Struts 2 upload functionality? – Dave Newton Sep 08 '13 at 17:54

1 Answers1

0

I found a solution to the problem I faced @

http://grokbase.com/t/struts/user/095ewvh02e/file-upload-jupload-how-to-disable-struts2-fileupload-interceptor

The thing which worked from above post :) -

I finally safely changed the struts2 filter mappings from "/" to ".action" and today i've had great success merging redback struts2 with my existing webapp.

I gave this a try & the S2 fileupload interceptor did not interfere & all my actions which use S2 have *.action extension.

Thanks Roman C & Dave for your time & efforts I appreciate

pshinde31

pshinde31
  • 43
  • 8