0

I wrote a simple page with a form to upload a file and a jsp to handle that file.

My problem is that when the file is uploaded, the request object is handled by Struts interceptors and when it comes to the jsp page it is already "consumed", so the calls to read the request with methods such as "ServletFileUpload.parseRequest()" returns empty lists.

I have already found a working solution, but this needs to restart Tomcat, and since the page has to be added on the production server, it would be so much better if there is a way to not restart it.

For now, what I tried and worked is this:
1) in struts.xml i added

<include file="custom/struts-custom.xml"/>

2) in the struts-custom.xml i wrote:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
        <constant name="struts.action.excludePattern" value="/path_to_my_jsp/page.jsp"/>
    </struts>

With this i can upload the file bypassing struts interceptors.

Is there a better / cleaner solution? as i said, the best would be a solution that doesn't need to restart the application server.

Even if i don't think that the problem is related to code, i will post it.

page.html:

<form action="upload.jsp" method="post" enctype="multipart/form-data" TARGET="Report_Down"
onSubmit="if(document.getElementById('file1').value == '') return false;">
    Input File <input type="file" name="file1" id="file1">
    <input type="submit" value="Upload">
</form>

upload.jsp:

boolean isMultipart = ServletFileUpload.isMultipartContent(request);

if (!isMultipart) {
    return;
}

DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024 * 1024 * 2);
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
String uploadFolder = getServletContext().getRealPath("")
        + File.separator + DATA_DIRECTORY;

ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(1024 * 1024);

try {
    List items = upload.parseRequest(request);

    Iterator iter = items.iterator();
    while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();

        if (!item.isFormField()) {
            try {
                String fileName = new File(item.getName()).getName();
                String filePath = uploadFolder + File.separator + fileName;
                File uploadedFile = new File(filePath);
                item.write(uploadedFile);

                //do something...
            }
            catch(Exception e) {
                //...
            }
        }
    }

getServletContext().getRequestDispatcher(forwardUrl).forward(
            request, response);

} catch (FileUploadException ex) {
    throw new ServletException(ex);
} catch (Exception ex) {
    throw new ServletException(ex);
}

Thank you in advance :)

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
giocarmine
  • 580
  • 1
  • 13
  • 29
  • Is that a... _scriptlet_? Wow. How very Java 5. Okay, where's you _actual_ code? I mean - you're obviously [not using scriptlets in production](http://stackoverflow.com/a/3180202/2071828) in 2016 right... – Boris the Spider Feb 04 '16 at 10:29
  • you would be surprised to know that on that server we have achieved to have JDK 6 installed only recently – giocarmine Feb 04 '16 at 10:46
  • JSP was introduced in Java EE 1.2 (1999), taglibs where added soon after in Java EE 1.3 (2001). Sriptlets were almost immediately considered extremely bad practice. I doubt there are many people on this site who have written scriptles in production after 2005. In short - getting help with this is going to be tricky because it is **very** obsolete. And in fact, your "new" code should not be using scriptlets. – Boris the Spider Feb 04 '16 at 11:00
  • Why do you want to parse the request manually? – Dave Newton Feb 05 '16 at 15:05

1 Answers1

0

What do you mean with the request is consumed ? The request passes through the Interceptor Stack up to the Action, and is still there.
If, for some reason (and you should not have one, when doing a simple upload of a file), you need to run code out of the framework mechanisms (Actions, Interceptors etc), you can use a Servlet.

By the way, this is the correct way of uploading a file in Struts2, and your problem is mostly related to

  1. the maximum file/request cap hit;
  2. a wrong definition of the interceptor stack.

I bet on n.1, but if this still haven't solved your problem, post more details about your struts.xml.

Also remember to avoid using scriptlets (as already suggested by Boris) and to never call JSPs directly: always pass through an Action.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thank you for your answer. I know this is an horrible practice, but the problem is that i want to bypass Struts, so i don't want to pass through an Action. I could request to restart Tomcat, but i can't add new Actions because that would require an upgrade of the deployed jar of the webapp and it can't be done, not in short times.. – giocarmine Feb 04 '16 at 13:25
  • Mettiti di traverso, nel 2016 nun se pò vede' :( :@ – Andrea Ligios Feb 04 '16 at 13:31