ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
<%@ 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."
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
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?
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.