LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-30-2022, 10:54 AM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,571

Rep: Reputation: 177Reputation: 177
Cannot compile for FileUpload class


Because of problems using org.apache.commons.fileupload.* with tomcat 10 (see https://www.linuxquestions.org/quest...-a-4175710078/) I am trying to instead use org.apache.tomcat.util.http.fileupload.*. I'm referring to the class definition: https://tomcat.apache.org/tomcat-10....ileUpload.html. I am importing:
Code:
<%@ page import="org.apache.tomcat.util.http.fileupload.*" %>

// and have the code:

boolean isMultipart = (contentType.indexOf("multipart/form-data") >= 0) ? true : false;

if (isMultipart)
{
    FileItemFactory factory = new getFileItemFactory();   // Create a factory for disk-based file items
    FileUpload upload = new FileUpload(factory); // Create a new file upload handler
I get the error:
Code:
An error occurred at line: [228] in the jsp file: [/1099R-Etrans.jsp]
getFileItemFactory cannot be resolved to a type
225: 
226: if (isMultipart)
227: {
228:     FileItemFactory factory = new getFileItemFactory();   // Create a factory for disk-based file items
229:     FileUpload upload = new FileUpload(factory); // Create a new file upload handler
230:     List items = upload.parseRequest(request);                 // Parse the request
231:     Iterator iter = items.iterator();                          // Process the uploaded items
I've also tried 'new FileItemFactory()' instead of 'new getFileItemFactory()' but I get the error "Cannot instantiate the type FileItemFactory."

What am I doing wrong?
 
Old 03-30-2022, 11:05 AM   #2
CrimsonEvenfall
LQ Newbie
 
Registered: Mar 2022
Distribution: Venom
Posts: 12

Rep: Reputation: 2
Did you try to place your jars in WEB-INF/lib? It seems a common issue.
https://bytes.com/topic/java/answers...ages-using-jsp
https://stackoverflow.com/questions/...apache-commons
 
Old 03-30-2022, 11:08 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,923

Rep: Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319
I guess there is no need to use new with getFileItemFactory():
Code:
228:     FileItemFactory factory = getFileItemFactory();   // Create a factory for disk-based file items
 
1 members found this post helpful.
Old 03-31-2022, 08:43 PM   #4
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,571

Original Poster
Rep: Reputation: 177Reputation: 177
Quote:
Originally Posted by CrimsonEvenfall View Post
Did you try to place your jars in WEB-INF/lib? It seems a common issue.
https://bytes.com/topic/java/answers...ages-using-jsp
https://stackoverflow.com/questions/...apache-commons
According to this page: https://www.mulesoft.com/tcat/tomcat-classpath (one of the clearest I've found so far), Tomcat does use WEB-INF, but does automatically load $CATALINA_HOME/lib, so I should be getting the correct jars:
Quote:
1. The JVM bootstrap loader loads the core Java libraries. Incidentally, this is the one place where environment variables do matter, as the JVM locates the core libraries using the JAVA_HOME variable.

2. Startup.sh, calling Catalina.sh with the "start" parameter, overwrites the system classpath and loads bootstrap.jar and tomcat-juli.jar. These resources are only visible to Tomcat.

3. Class loaders are created for each deployed Context, which load all classes and JAR files contained in each web application's WEB-INF/classes and WEB-INF/lib, respectively and in that order. These resources are only visible to the web application that loads them.

4. The Common class loader loads all classes and JAR files contained in $CATALINA_HOME/lib. These resources are visible to all applications and to Tomcat.
Quote:
Originally Posted by pan64 View Post
I guess there is no need to use new with getFileItemFactory():
Code:
228:     FileItemFactory factory = getFileItemFactory();   // Create a factory for disk-based file items
That didn't work either:
Code:
An error occurred at line: [228] in the jsp file: [/1099R-Etrans.jsp]
The method getFileItemFactory() is undefined for the type _1099R_002dEtrans_jsp
225: 
226: if (isMultipart)
227: {
228:     FileItemFactory factory = getFileItemFactory();   // Create a factory for disk-based file items
229:     FileUpload upload = new FileUpload(factory); // Create a new file upload handler
230:     List items = upload.parseRequest(request);                 // Parse the request
231:     Iterator iter = items.iterator();                          // Process the uploaded items
I've subscribed to the Commons Developers List <dev@commons.apache.org> and asked this javax/jakarta question there in the [fileupload] topic. So far, no joy, but I'll keep on that.

However, the package I'm trying to use now is org.apache.tomcat.util.http.fileupload, not the org.apache.commons.fileupload. So, there is something else afoot than the javax/jakarta issue.

Maybe Tomcat 10.0 is a version whose time has not yet come? Has anyone actually implemented 10.0? With FileUpload?

Last edited by mfoley; 03-31-2022 at 08:47 PM.
 
Old 04-06-2022, 06:24 PM   #5
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,571

Original Poster
Rep: Reputation: 177Reputation: 177
I've made some modest progress on this. I'm trying to use the org.apache.tomcat.util.http.fileupload package, which does exist in $CATALINA_HOME/lib/tomcat-coyote.jar by installation default. I'm following the example shown here https://tomcat.apache.org/tomcat-8.5...e-summary.html. I have:
Code:
<%@ page import="org.apache.tomcat.util.http.fileupload.disk.*,
  org.apache.tomcat.util.http.fileupload.servlet.*" %>
:
    DiskFileItemFactory factory = new DiskFileItemFactory();   // Create a factory for disk-based file items
    ServletFileUpload upload = new ServletFileUpload(factory); // Create a new file upload handler
    List items = upload.parseRequest(request);                 // Parse the request
This is identical to the org/apache/commons/fileupload package. Now it compiles past the factory and upload constructors, but I get an error on 'upload.parseRequest(request).
Code:
An error occurred at line: [231] in the jsp file: [/1099R-Etrans.jsp]
The method parseRequest(RequestContext) in the type FileUploadBase is not applicable for the arguments (HttpServletRequest)
228: {
229:     DiskFileItemFactory factory = new DiskFileItemFactory();   // Create a factory for disk-based file items
230:     ServletFileUpload upload = new ServletFileUpload(factory); // Create a new file upload handler
231:     List items = upload.parseRequest(request);                 // Parse the request
232:     Iterator iter = items.iterator();                          // Process the uploaded items
233:     FileItem item = null;
234:
Supposedly, "The JSP request is an implicit object of type HttpServletRequest" (https://www.javatpoint.com/request-implicit-object). According to the example I've referenced (and the error message) upload.parseRequest does take a HttpServletRequest argument. However, the error message is saying that the JSP request is type RequestContext, not HttpServletRequest.

Can Java/JSP expert help me on this? can I cast/covert my 'request' object to HttpServletRequest?

Last edited by mfoley; 04-06-2022 at 06:26 PM.
 
Old 04-07-2022, 01:31 AM   #6
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,571

Original Poster
Rep: Reputation: 177Reputation: 177
Solved! I was able to convert my Request HttpServletRequest to a RequestContext, credit goes to this post: https://stackoverflow.com/questions/...ttpservletrequ. Then it was a matter of doing more web searchs to find the class definitions I needed to import. The following now works:
Code:
<%@ page import="org.apache.tomcat.util.http.fileupload.*,
  org.apache.tomcat.util.http.fileupload.disk.*,
  org.apache.tomcat.util.http.fileupload.servlet.*" %>
:
boolean isMultipart = FileUpload.isMultipartContent(new ServletRequestContext(request));

if (isMultipart)
{
    DiskFileItemFactory factory = new DiskFileItemFactory();   // Create a factory for disk-based file items
    ServletFileUpload upload = new ServletFileUpload(factory); // Create a new file upload handler
        
    List items = upload.parseRequest(new ServletRequestContext(request));
    Iterator iter = items.iterator();                          // Process the uploaded items
    FileItem item = null;
So, my upgrade to Tomcat 10 is finally complete.

I heartily wish that when people gave example HowTo Java code on the Web that they would specify which class definitions were needed. It seems they almost never do. I end up spending significant time searching multiple sites to figure out what I need to import. I hope this post helps someone with the same problem, and I have made sure to include the imports I needed.

Thanks to all.

Last edited by mfoley; 04-07-2022 at 01:32 AM.
 
  


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
BlackBox.class & VerifierBug.class virus ??? dalek Linux - Security 4 02-29-2004 08:55 AM
Inheriting class members (Qt C++, QApplication class) jtshaw Programming 2 01-15-2004 11:52 AM
Communicating Class A and Class C Networks chadtce Linux - Networking 10 07-23-2003 01:36 PM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 06:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:12 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