LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   FileUpload class not working with Tomcat 10.1 (https://www.linuxquestions.org/questions/linux-server-73/fileupload-class-not-working-with-tomcat-10-1-a-4175729983/)

mfoley 10-17-2023 07:20 PM

FileUpload class not working with Tomcat 10.1
 
I recently upgreaded from Tomcat 10.0.17 to 10.1.13. When I previously upgraded from 9.0.41 to 10.0.17 (back in 2/22) the FileUpload class broke. I fixed that (https://www.linuxquestions.org/quest...ss-4175710186/), but now that I've upgraded to 10.1.13 it is broken again! Here's the error I get:
Code:

> An error occurred at line: [40] in the jsp file: [/schDistImportResults.jsp]
> The method isMultipartContent(ServletRequestContext) is undefined for the type FileUpload
> 37: String programName = "$RCSfile: schDistImportResults.jsp,v $";
> 38: programName = programName.substring(programName.indexOf(':') + 2,programName.indexOf(','));
> 39:
> 40: boolean isMultipart = FileUpload.isMultipartContent(new ServletRequestContext(request));
> 41:
>
> An error occurred at line: [133] in the jsp file: [/schDistImportResults.jsp]
> ServletFileUpload cannot be resolved to a type
> 130: stmt = con.createStatement();
> 131:
> 132: DiskFileItemFactory factory = new DiskFileItemFactory();  // Create a factory for disk-based file items
> 133: ServletFileUpload upload = new ServletFileUpload(factory); // Create a new file upload handler
> 134: List items = upload.parseRequest(new ServletRequestContext(request));                // Parse the request
> 135: Iterator iter = items.iterator();                          // Process the uploaded items
> 136: FileItem item = null;
>

I've checked the RELEASE-NOTES, FAQ and searched the web. I've checked the UploadFile class (no clue) and looked for examples, but none resembled my app. I tried reverting back to the program version I had with 9.0.41, but that didn't work.

Here is all I changed in the program between Tomcat versions 9.0.41 and 10.0.17 (which worked):
Code:

26,28c26,28
< <%@ page import="org.apache.commons.fileupload.*,
<    org.apache.commons.fileupload.disk.*,
<    org.apache.commons.fileupload.servlet.*,
---
> <%@ page import="org.apache.tomcat.util.http.fileupload.*,
>    org.apache.tomcat.util.http.fileupload.disk.*,
>    org.apache.tomcat.util.http.fileupload.servlet.*,
40c40
< boolean isMultipart = FileUpload.isMultipartContent(request);
---
> boolean isMultipart = FileUpload.isMultipartContent(new ServletRequestContext(request));
134c134
< List items = upload.parseRequest(request);                // Parse the request
---
> List items = upload.parseRequest(new ServletRequestContext(request));                // Parse the request

Does anyone know how to fix this latest breakage?

mfoley 11-27-2023 02:02 PM

Thanks to Thomas Hoffmann on the Tomcat maillist, I have the solution. The following works:
Code:

<%@ page import="jakarta.servlet.annotation.MultipartConfig.*" %>

if((contentType != null) && contentType.startsWith("multipart/form-data;"))
{
    InputStream inp = null;
    DataInputStream ins = null;

    Part fileUpload = request.getPart("taxResults");    // <input> tag name of type="file"

    if(fileUpload != null)
    {
        inp = fileUpload.getInputStream();
        ins = new DataInputStream(inp);
    }

    while ((inp != null) && (ins.available() != 0))
    {
        String  transaction = ins.readLine();
        out.println("<br/>" + transaction);
    }

    ins.close();
    inp.close();
}

Of course the <form> must have enctype="multipart/form-data":
Code:

<form name="importForm" method="post" enctype="multipart/form-data" action="<%=programName%>">
In addition, it is critical to add some tags to WEB-INF/web.xml:
Code:

<servlet>
  <servlet-name>uploadfile</servlet-name>
    <jsp-file>/myapp.jsp</jsp-file>
    <multipart-config>
      <location>/tmp</location>
      <max-file-size>20848820</max-file-size>
      <max-request-size>418018841</max-request-size>
      <file-size-threshold>1048576</file-size-threshold>
    </multipart-config>
</servlet>

<servlet-mapping>
  <servlet-name>uploadfile</servlet-name>
  <url-pattern>/myapp.jsp</url-pattern>
</servlet-mapping>

The jsp-file needs to be in there. A <servlet> tag is needed for each jsp doing fileUpload. However, I haven't yet tried configuring more than one jsp program. I will soon. Not exactly sure how that will work with the <servlet-mapping> tag.


All times are GMT -5. The time now is 11:01 AM.