LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-17-2023, 07:20 PM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,572

Rep: Reputation: 177Reputation: 177
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?

Last edited by mfoley; 10-17-2023 at 10:38 PM.
 
Old 11-27-2023, 02:02 PM   #2
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,572

Original Poster
Rep: Reputation: 177Reputation: 177
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.

Last edited by mfoley; 11-27-2023 at 02:06 PM.
 
  


Reply

Tags
fileupload, tomcat10



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] FileUpload class not working with Tomcat 10 mfoley Linux - Server 9 04-13-2022 12:11 PM
[SOLVED] Cannot compile for FileUpload class mfoley Programming 5 04-07-2022 01:31 AM
[SOLVED] TOMCAT ERROR:Error: Could not find or load main class org.apache.catalina.startup.Bootstrap businesscat Linux - Newbie 2 01-03-2018 10:06 AM
[SOLVED] Compilation error trying to sub-class a Qt QAction class, not sure why rstewart Programming 2 02-08-2011 11:45 AM
TOMCAT init script not working on startup -- tomcat 4.x / Mandrake Linux 8.0 jmartinph Mandriva 0 03-08-2004 01:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration