LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Cannot compile for FileUpload class (https://www.linuxquestions.org/questions/programming-9/cannot-compile-for-fileupload-class-4175710186/)

mfoley 03-30-2022 10:54 AM

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?

CrimsonEvenfall 03-30-2022 11:05 AM

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

pan64 03-30-2022 11:08 AM

I guess there is no need to use new with getFileItemFactory():
Code:

228:    FileItemFactory factory = getFileItemFactory();  // Create a factory for disk-based file items

mfoley 03-31-2022 08:43 PM

Quote:

Originally Posted by CrimsonEvenfall (Post 6342732)
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 (Post 6342733)
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?

mfoley 04-06-2022 06:24 PM

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?

mfoley 04-07-2022 01:31 AM

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.


All times are GMT -5. The time now is 03:17 PM.