You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mark Foley <mf...@novatec-inc.com> on 2023/11/10 16:49:45 UTC

FileUpload class not working with Tomcat 10.1

I recently upgraded 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
thanks to postings on stackoverflow, but now that I've
upgraded to 10.1.13 it is broken again! Here's the error I get:

An error occurred at line: [40] in the jsp file: [/schDistImportResults.jsp]
The method isMultipartContent(ServletRequestContext) is undefined for the type FileUpload

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

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

I have quite a few programs that use the FileUpload methods. Does anyone know how to fix this latest breakage?

Thanks --Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: AW: FileUpload class not working with Tomcat 10.1

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Mark,

On 4/23/24 13:42, Mark Foley wrote:
> I'm back with a related issue.
> 
> I was able to get the java class jakarta.servlet.annotation.MultipartConfig
> working back last November by adding the <servlet> definition shown in the
> included message below to my WEB-INF/web.xml file.
> 
> Now I need to add another program to the system that does file uploads. I
> created another <server> definition in WEB-INF/web.xml following the original:
> 
> <servlet>
>     <servlet-name>uploadfile</servlet-name>
>       <jsp-file>/1099R-Etrans.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>/1099R-Etrans.jsp</url-pattern>
> </servlet-mapping>
> 
> That didn't work so well.  Now, any and all programs using the fileupload
> function launches this 2nd program 1099R-Etrans.jsp.  It appears that this
> second <servlet> definition replaces the first. Of course, I need to have the
> ability for more than one program in the system able to do file uploads.
> 
> How can I configure multiple JSP programs to all have file uploading enabled and
> launched by the correspoding requesting program?

Can you post both <servlet> definitions and the <servlet-mapping>s for 
each of them?

It's still yucky to use a JSP to do this IMHO.

-chris

> On Thu Nov 16 14:36:21 2023 Christopher Schultz <ch...@christopherschultz.net> wrote:
>> Mark,
>>
>> Apologies for not replying earlier; looks like you have made good
>> progress. See below.
>>
>> On 11/14/23 12:19, Mark Foley wrote:
>>> Anyway, enough griping! I have gotten it partially working thanks to your
>>> suggested link, and particulary you suggestion to put the servlet info in
>>> web.xml.  I've put the following in WEB-INF/web.xml:
>>>
>>> <servlet>
>>>     <servlet-name>uploadfile</servlet-name>
>>>       <jsp-file>/schDistImportResults.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>/schDistImportResults.jsp</url-pattern>
>>> </servlet-mapping>
>>>
>>> I've only changed the <jsp-file> and <url-pattern> tags above. The others are
>>> as monkey-typed from your link example. I'll research the other parameters
>>> later.
>>>
>>> My jsp code is now:
>>>
>>> <%@ page import="javax.servlet.annotation.MultipartConfig.*" %>
>>
>> Nope, not for Tomcat 10. You need to use the jakarta package names.
>> Besides, you don't need the MultipartConfig in your code, anyway.
>>
>> You need /either/ an annotation (dicey in JSP code) /or/ an XML config,
>> so the XML should be sufficient. (But you should switch to a proper
>> servlet. DO IT! :)
>>
>>> if((contentType != null) && contentType.startsWith("multipart/form-data;"))
>>> {
>>>       InputStream inp = null;
>>>       DataInputStream ins = null;
>>>
>>>       Part fileUpload = request.getPart("taxResults");
>>>
>>>       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();
>>
>> I would use try-with-resources like this:
>>
>> try (InputStream in = , DataInputStream ins = ...) {
>> }
>>
>> Since you have no try/catch, your code can leak file handles and stuff
>> like that. Yuck. With try-with-resources, you don't even need the calls
>> to InputStream.close.
>>
>>> This actually worked!!!! I will experiment with it more and may be back with
>>> more questions (e.g. do I really need the web.xml? Could I not do:
>>> "inp = fileUpload.getInputStream(mypath);"). But ... maybe later.
>>>
>>> Vielen Dank!!! --Mark
>>
>> Na klar
>>
>> -chris
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: FileUpload class not working with Tomcat 10.1

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Mark and Chuck,

On 5/9/24 09:35, Chuck Caldarale wrote:
> You need the web.xml entries because you have extra configuration
> items (the <multipart-config> settings) that aren’t part of the
> default JSP servlet definition.
+1

If you didn't need to upload files to your JSP, you wouldn't have needed 
any of this in your web.xml file.

It's very weird to do this kind of logic in a JSP. I *highly* recommend 
that you split your JSP into at least two pieces:

1. A servlet that handles the upload, produces no output, and handles 
error conditions gracefully. It then forwards or redirects (as 
appropriate) to the page you want to display post-upload. You will need 
to map this servlet in web.xml, but it's less-stupid than mapping a JSP 
to a servlet-name and then mapping that same servlet-name back to a URL 
pattern which is the same as the JSP's path. I can see why you were 
saying "I have no idea why this is necessary": it seems useless but you 
must attach the file-upload metadata to something, and this is how you 
do it.

Note that you didn't have to do it that way. You could have done this:

<servlet>
   <servlet-name>uploadfile</servlet-name>
   <jsp-file>/schDistImports.jsp</jsp-file>
   ...
</servlet>
<servlet-mapping>
   <servlet-name>uploadfile</servlet-mapping>
   <url-pattern>/schDistImports</url-pattern> NOTE: no .jsp extension
</servlet-mapping>

In your case, the generic name "uploadfile" for a very specific type of 
upload (schDistImports) might be a mistake, since you might want to 
upload all kinds of files, such as 1099 forms or whatnot. One called 
"uploadfile" seems generic when it's not really generic: it's specific 
to that one workflow.

You can use any name you like. You can use any URL pattern you like as 
well, such as /sch/dist/imports. You don't have to be tied to your 
filesystem layout.

2. A page template (JSP is fine) that only generates page content. No 
mapping in web.xml is necessary for this, which is probably what you are 
used to.

-chris

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: FileUpload class not working with Tomcat 10.1

Posted by Chuck Caldarale <n8...@gmail.com>.
> On May 9, 2024, at 01:25, Mark Foley <mf...@novatec-inc.com> wrote:
> 
>> Does the JSP need to reference the "program" (servlet?) at all? 
> The program, as shown above didn'twork at all until I put that servlet definition on WEB-INF/web.xml, so I suppose the answer is "yes". As to why, I have not a clue.


A reading of the servlet spec might be in order here. Servlets (including JSPs) are selected based on the mapping of the <url-pattern> to the <servlet-name>.


>> When you make a request, Tomcat determines which servlet in your application will service the request. If that's a JSP, then the JSP is invoked. A JSP just compiles to a servlet, just as if you had written a .java file with a class that "extends HttpServlet" or similar.
>> 
>> It's not clear what "the program" is: JSP or servlet? Or something else? 
> The programs are written in Java/JSP and, yes, Tomcat "compiles" them to .class -- probably servlets.


No probably about it - JSPs are always compiled into servlets. “Program” is too generic a term to be used here - you need to be specific with what you’re talking about: servlets you coded and compiled, or JSPs that Tomcat turns into servlets. It’s hard to figure out exactly what you’re really talking about.


> I think I may have figured this out. Here are my two servlet definitions in WEB-INF/web.xml:
> 
>   <servlet>
>               <servlet-name>uploadfile</servlet-name>
>   <jsp-file>/schDistImportResults.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>/schDistImportResults.jsp</url-pattern>
>   </servlet-mapping>
> 
>   <servlet>
>               <servlet-name>*upload1099*</servlet-name>


I presume the asterisks are not actually present in your config.


>               <jsp-file>/1099R-Etrans.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>*upload1099*</servlet-name>
>   <url-pattern>/1099R-Etrans.jsp</url-pattern>
>   </servlet-mapping>
> 
> In the 2nd definition, Taking Chuck's hint, I changed the servlet-name to "upload1099". That seemed to work for the 1099R-Etrans.jsp program, but I haven't been able to test the schDistImportResults.jsp program yet to see if I broke that one. Why these definitions are needed in web.xml and how all that works under the hood is, as Chuck said, "magic”.


It’s not magic at all - it’s how servlet selection works, as defined in the servlet spec. The “magic” was your expectation that servlets with the same name could co-exist. You need the web.xml entries because you have extra configuration items (the <multipart-config> settings) that aren’t part of the default JSP servlet definition.

  - Chuck


Re: FileUpload class not working with Tomcat 10.1

Posted by Mark Foley <mf...@novatec-inc.com>.
On 5/7/2024 4:52 PM, Christopher Schultz wrote:
> Mark,
>
> On 5/3/24 12:16, Mark Foley wrote:
>>
>> On 4/23/24 18:44, Chuck Caldarale wrote:
>>>>>> <servlet>
>>>>>>    <servlet-name>uploadfile</servlet-name>
>>> <snip/>
>>>
>>>>>> </servlet>
>>>>>> <servlet-mapping>
>>>>>>    <servlet-name>uploadfile</servlet-name>
>>>>>> <url-pattern>/schDistImportResults.jsp</url-pattern>
>>>>>> </servlet-mapping>
>>> The first servlet is named “uploadfile”.
>>>
>>>> On Apr 23, 2024, at 12:42, Mark Foley<mf...@novatec-inc.com>  wrote:
>>>>
>>>> Now I need to add another program to the system that does file 
>>>> uploads. I
>>>> created another <server> definition in WEB-INF/web.xml following 
>>>> the original:
>>>>
>>>> <servlet>
>>>>    <servlet-name>uploadfile</servlet-name>
>>> <snip/>
>>>
>>>> </servlet>
>>>> <servlet-mapping>
>>>>    <servlet-name>uploadfile</servlet-name>
>>>>    <url-pattern>/1099R-Etrans.jsp</url-pattern>
>>>> </servlet-mapping>
>>> This second servlet is also named “uploadfile”.
>>>
>>>> That didn't work so well.  Now, any and all programs using the 
>>>> fileupload
>>>> function launches this 2nd program 1099R-Etrans.jsp.  It appears 
>>>> that this
>>>> second <servlet> definition replaces the first.
>>> You gave them the same names, so the second one wins...
>>>
>>> What magic were you expecting to differentiate between the two?
>>>
>>>    - Chuck
>>>
>>> I can easily change the name of the second servlet, but how would 
>>> the respective jsp programs (schDistImportResults.jsp, 
>>> 1099R-Etrans.jsp) specify one or the other? The programs do:
>> String contentType = request.getContentType();
>>
>> if (contentType.startsWith("multipart/form-data;"))
>> {
>>      Part fileUpload = request.getPart("taxResults");  // for 
>> schDistImportResults.jsp
>> // or
>>      Part fileUpload = request.getPart("vendor1099-MISC"); // for 
>> 1099R-Etrans.jsp
>>
>>      InputStream inFile = fileUpload.getInputStream();
>>   :
>> }
>>
>> That's it. There is nothing in the program that specifies a servlet 
>> name. My initial servlet definition (for schDistImportResults.jsp) 
>> was based on the XML suggestion from Christopher Schultz back in 
>> November, 2023. Since only the one jsp program was involved, there 
>> was no discussion of how to specify more than one program in web.xml.
>>
>> So, I can (and will) give the servlets different names in web.xml, 
>> but how does the jsp program select the one for its use?
>
> Does the JSP need to reference the "program" (servlet?) at all? 
The program, as shown above didn'twork at all until I put that servlet 
definition on WEB-INF/web.xml, so I suppose the answer is "yes". As to 
why, I have not a clue.

> When you make a request, Tomcat determines which servlet in your 
> application will service the request. If that's a JSP, then the JSP is 
> invoked. A JSP just compiles to a servlet, just as if you had written 
> a .java file with a class that "extends HttpServlet" or similar.
>
> It's not clear what "the program" is: JSP or servlet? Or something else? 
The programs are written in Java/JSP and, yes, Tomcat "compiles" them to 
.class -- probably servlets.

> It's also not clear how "the program" would or should reference a 
> servlet name.
>
> Maybe you can explain (again)?
>
> -chris
I think I may have figured this out. Here are my two servlet definitions 
in WEB-INF/web.xml:

    <servlet>
                <servlet-name>uploadfile</servlet-name>
    <jsp-file>/schDistImportResults.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>/schDistImportResults.jsp</url-pattern>
    </servlet-mapping>

    <servlet>
                <servlet-name>*upload1099*</servlet-name>
                <jsp-file>/1099R-Etrans.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>*upload1099*</servlet-name>
    <url-pattern>/1099R-Etrans.jsp</url-pattern>
    </servlet-mapping>

In the 2nd definition, Taking Chuck's hint, I changed the servlet-name 
to "upload1099". That seemed to work for the 1099R-Etrans.jsp program, 
but I haven't been able to test the schDistImportResults.jsp program yet 
to see if I broke that one. Why these definitions are needed in web.xml 
and how all that works under the hood is, as Chuck said, "magic".

Re: FileUpload class not working with Tomcat 10.1

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Mark,

On 5/3/24 12:16, Mark Foley wrote:
> 
> On 4/23/24 18:44, Chuck Caldarale wrote:
>>>>> <servlet>
>>>>>    <servlet-name>uploadfile</servlet-name>
>> <snip/>
>>
>>>>> </servlet>
>>>>> <servlet-mapping>
>>>>>    <servlet-name>uploadfile</servlet-name>
>>>>>    <url-pattern>/schDistImportResults.jsp</url-pattern>
>>>>> </servlet-mapping>
>> The first servlet is named “uploadfile”.
>>
>>> On Apr 23, 2024, at 12:42, Mark Foley<mf...@novatec-inc.com>  wrote:
>>>
>>> Now I need to add another program to the system that does file 
>>> uploads. I
>>> created another <server> definition in WEB-INF/web.xml following the 
>>> original:
>>>
>>> <servlet>
>>>    <servlet-name>uploadfile</servlet-name>
>> <snip/>
>>
>>> </servlet>
>>> <servlet-mapping>
>>>    <servlet-name>uploadfile</servlet-name>
>>>    <url-pattern>/1099R-Etrans.jsp</url-pattern>
>>> </servlet-mapping>
>> This second servlet is also named “uploadfile”.
>>
>>> That didn't work so well.  Now, any and all programs using the 
>>> fileupload
>>> function launches this 2nd program 1099R-Etrans.jsp.  It appears that 
>>> this
>>> second <servlet> definition replaces the first.
>> You gave them the same names, so the second one wins...
>>
>> What magic were you expecting to differentiate between the two?
>>
>>    - Chuck
>>
>> I can easily change the name of the second servlet, but how would the 
>> respective jsp programs (schDistImportResults.jsp, 1099R-Etrans.jsp) 
>> specify one or the other? The programs do:
> String contentType = request.getContentType();
> 
> if (contentType.startsWith("multipart/form-data;"))
> {
>      Part fileUpload = request.getPart("taxResults");  // for 
> schDistImportResults.jsp
> // or
>      Part fileUpload = request.getPart("vendor1099-MISC"); // for 
> 1099R-Etrans.jsp
> 
>      InputStream inFile = fileUpload.getInputStream();
>   :
> }
> 
> That's it. There is nothing in the program that specifies a servlet 
> name. My initial servlet definition (for schDistImportResults.jsp) was 
> based on the XML suggestion from Christopher Schultz back in November, 
> 2023. Since only the one jsp program was involved, there was no 
> discussion of how to specify more than one program in web.xml.
> 
> So, I can (and will) give the servlets different names in web.xml, but 
> how does the jsp program select the one for its use?

Does the JSP need to reference the "program" (servlet?) at all? When you 
make a request, Tomcat determines which servlet in your application will 
service the request. If that's a JSP, then the JSP is invoked. A JSP 
just compiles to a servlet, just as if you had written a .java file with 
a class that "extends HttpServlet" or similar.

It's not clear what "the program" is: JSP or servlet? Or something else? 
It's also not clear how "the program" would or should reference a 
servlet name.

Maybe you can explain (again)?

-chris

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: FileUpload class not working with Tomcat 10.1

Posted by Mark Foley <mf...@novatec-inc.com>.
On 4/23/24 18:44, Chuck Caldarale wrote:
>>>> <servlet>
>>>>    <servlet-name>uploadfile</servlet-name>
> <snip/>
>
>>>> </servlet>
>>>> <servlet-mapping>
>>>>    <servlet-name>uploadfile</servlet-name>
>>>>    <url-pattern>/schDistImportResults.jsp</url-pattern>
>>>> </servlet-mapping>
> The first servlet is named “uploadfile”.
>
>> On Apr 23, 2024, at 12:42, Mark Foley<mf...@novatec-inc.com>  wrote:
>>
>> Now I need to add another program to the system that does file uploads. I
>> created another <server> definition in WEB-INF/web.xml following the original:
>>
>> <servlet>
>>    <servlet-name>uploadfile</servlet-name>
> <snip/>
>
>> </servlet>
>> <servlet-mapping>
>>    <servlet-name>uploadfile</servlet-name>
>>    <url-pattern>/1099R-Etrans.jsp</url-pattern>
>> </servlet-mapping>
> This second servlet is also named “uploadfile”.
>
>> That didn't work so well.  Now, any and all programs using the fileupload
>> function launches this 2nd program 1099R-Etrans.jsp.  It appears that this
>> second <servlet> definition replaces the first.
> You gave them the same names, so the second one wins...
>
> What magic were you expecting to differentiate between the two?
>
>    - Chuck
>
> I can easily change the name of the second servlet, but how would the respective jsp programs (schDistImportResults.jsp, 1099R-Etrans.jsp) specify one or the other? The programs do:
String contentType = request.getContentType();

if (contentType.startsWith("multipart/form-data;"))
{
     Part fileUpload = request.getPart("taxResults");  // for 
schDistImportResults.jsp
// or
     Part fileUpload = request.getPart("vendor1099-MISC"); // for 
1099R-Etrans.jsp

     InputStream inFile = fileUpload.getInputStream();
  :
}

That's it. There is nothing in the program that specifies a servlet 
name. My initial servlet definition (for schDistImportResults.jsp) was 
based on the XML suggestion from Christopher Schultz back in November, 
2023. Since only the one jsp program was involved, there was no 
discussion of how to specify more than one program in web.xml.

So, I can (and will) give the servlets different names in web.xml, but 
how does the jsp program select the one for its use?

Thanks --Mark

Re: FileUpload class not working with Tomcat 10.1

Posted by Chuck Caldarale <n8...@gmail.com>.
>>> <servlet>
>>>   <servlet-name>uploadfile</servlet-name>

<snip/>

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



The first servlet is named “uploadfile”.


> On Apr 23, 2024, at 12:42, Mark Foley <mf...@novatec-inc.com> wrote:
> 
> Now I need to add another program to the system that does file uploads. I
> created another <server> definition in WEB-INF/web.xml following the original:
> 
> <servlet>
>   <servlet-name>uploadfile</servlet-name>

<snip/>

> </servlet>
> <servlet-mapping>
>   <servlet-name>uploadfile</servlet-name>
>   <url-pattern>/1099R-Etrans.jsp</url-pattern>
> </servlet-mapping>



This second servlet is also named “uploadfile”.


> That didn't work so well.  Now, any and all programs using the fileupload
> function launches this 2nd program 1099R-Etrans.jsp.  It appears that this
> second <servlet> definition replaces the first.



You gave them the same names, so the second one wins...

What magic were you expecting to differentiate between the two?

  - Chuck


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: AW: FileUpload class not working with Tomcat 10.1

Posted by Mark Foley <mf...@novatec-inc.com>.
I'm back with a related issue.

I was able to get the java class jakarta.servlet.annotation.MultipartConfig
working back last November by adding the <servlet> definition shown in the
included message below to my WEB-INF/web.xml file. 

Now I need to add another program to the system that does file uploads. I
created another <server> definition in WEB-INF/web.xml following the original:

<servlet>
   <servlet-name>uploadfile</servlet-name>
     <jsp-file>/1099R-Etrans.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>/1099R-Etrans.jsp</url-pattern>
</servlet-mapping>

That didn't work so well.  Now, any and all programs using the fileupload
function launches this 2nd program 1099R-Etrans.jsp.  It appears that this
second <servlet> definition replaces the first. Of course, I need to have the
ability for more than one program in the system able to do file uploads.

How can I configure multiple JSP programs to all have file uploading enabled and
launched by the correspoding requesting program?

Thanks --Mark

On Thu Nov 16 14:36:21 2023 Christopher Schultz <ch...@christopherschultz.net> wrote:
> Mark,
>
> Apologies for not replying earlier; looks like you have made good 
> progress. See below.
>
> On 11/14/23 12:19, Mark Foley wrote:
> > Anyway, enough griping! I have gotten it partially working thanks to your
> > suggested link, and particulary you suggestion to put the servlet info in
> > web.xml.  I've put the following in WEB-INF/web.xml:
> > 
> > <servlet>
> >    <servlet-name>uploadfile</servlet-name>
> >      <jsp-file>/schDistImportResults.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>/schDistImportResults.jsp</url-pattern>
> > </servlet-mapping>
> > 
> > I've only changed the <jsp-file> and <url-pattern> tags above. The others are
> > as monkey-typed from your link example. I'll research the other parameters
> > later.
> > 
> > My jsp code is now:
> > 
> > <%@ page import="javax.servlet.annotation.MultipartConfig.*" %>
>
> Nope, not for Tomcat 10. You need to use the jakarta package names. 
> Besides, you don't need the MultipartConfig in your code, anyway.
>
> You need /either/ an annotation (dicey in JSP code) /or/ an XML config, 
> so the XML should be sufficient. (But you should switch to a proper 
> servlet. DO IT! :)
>
> > if((contentType != null) && contentType.startsWith("multipart/form-data;"))
> > {
> >      InputStream inp = null;
> >      DataInputStream ins = null;
> > 
> >      Part fileUpload = request.getPart("taxResults");
> > 
> >      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();
>
> I would use try-with-resources like this:
>
> try (InputStream in = , DataInputStream ins = ...) {
> }
>
> Since you have no try/catch, your code can leak file handles and stuff 
> like that. Yuck. With try-with-resources, you don't even need the calls 
> to InputStream.close.
>
> > This actually worked!!!! I will experiment with it more and may be back with
> > more questions (e.g. do I really need the web.xml? Could I not do:
> > "inp = fileUpload.getInputStream(mypath);"). But ... maybe later.
> > 
> > Vielen Dank!!! --Mark
>
> Na klar
>
> -chris
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: AW: FileUpload class not working with Tomcat 10.1

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Mark,

Apologies for not replying earlier; looks like you have made good 
progress. See below.

On 11/14/23 12:19, Mark Foley wrote:
> Anyway, enough griping! I have gotten it partially working thanks to your
> suggested link, and particulary you suggestion to put the servlet info in
> web.xml.  I've put the following in WEB-INF/web.xml:
> 
> <servlet>
>    <servlet-name>uploadfile</servlet-name>
>      <jsp-file>/schDistImportResultsX.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>/schDistImportResultsX.jsp</url-pattern>
> </servlet-mapping>
> 
> I've only changed the <jsp-file> and <url-pattern> tags above. The others are
> as monkey-typed from your link example. I'll research the other parameters
> later.
> 
> My jsp code is now:
> 
> <%@ page import="javax.servlet.annotation.MultipartConfig.*" %>

Nope, not for Tomcat 10. You need to use the jakarta package names. 
Besides, you don't need the MultipartConfig in your code, anyway.

You need /either/ an annotation (dicey in JSP code) /or/ an XML config, 
so the XML should be sufficient. (But you should switch to a proper 
servlet. DO IT! :)

> if((contentType != null) && contentType.startsWith("multipart/form-data;"))
> {
>      InputStream inp = null;
>      DataInputStream ins = null;
> 
>      Part fileUpload = request.getPart("taxResults");
> 
>      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();

I would use try-with-resources like this:

try (InputStream in = , DataInputStream ins = ...) {
}

Since you have no try/catch, your code can leak file handles and stuff 
like that. Yuck. With try-with-resources, you don't even need the calls 
to InputStream.close.

> This actually worked!!!! I will experiment with it more and may be back with
> more questions (e.g. do I really need the web.xml? Could I not do:
> "inp = fileUpload.getInputStream(mypath);"). But ... maybe later.
> 
> Vielen Dank!!! --Mark

Na klar

-chris

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: AW: AW: FileUpload class not working with Tomcat 10.1

Posted by Mark Foley <mf...@novatec-inc.com>.
On Tue Nov 14 14:50:10 2023 "Thomas Hoffmann (Speed4Trade GmbH)" Thomas.Hoffmann@speed4trade.com.INVALID> wrote:
>
> Hi Mark!
>
> > -----Ursprüngliche Nachricht-----
> > Von: Mark Foley <mf...@novatec-inc.com>
> > Gesendet: Dienstag, 14. November 2023 18:20
> > An: users@tomcat.apache.org
> > Betreff: Re: AW: FileUpload class not working with Tomcat 10.1
> > 
> > On Tue Nov 14 01:46:09 2023 "Thomas Hoffmann (Speed4Trade GmbH)"
> > <Th...@speed4trade.com.INVALID> wrote:
> > >
> > > Hello Mark,
> > >
> > > > -----Ursprüngliche Nachricht-----
> > > > Von: Mark Foley <mf...@novatec-inc.com>
> > > > Gesendet: Montag, 13. November 2023 23:12
> > > > An: users@tomcat.apache.org
> > > > Betreff: Re: AW: FileUpload class not working with Tomcat 10.1
> > > >
> > > > On Mon Nov 13 02:18:49 2023 "Thomas Hoffmann (Speed4Trade GmbH)"
> > > > <Th...@speed4trade.com.INVALID> wrote:
> > > > > Hello,
> > > > >
> > > > > > -----Ursprüngliche Nachricht-----
> > > > > > Von: Mark Foley <mf...@novatec-inc.com>
> > > > > > Gesendet: Sonntag, 12. November 2023 19:04
> > > > > > An: users@tomcat.apache.org
> > > > > > Betreff: Re: FileUpload class not working with Tomcat 10.1
> > > > > >
> > > > > > On Fri Nov 10 15:57:50 2023 Christopher Schultz
> > > > > > <ch...@christopherschultz.net> wrote:
> > > > > > >
> > > > > > > Mark,
> > > > > > >
> > > > > > > On 11/10/23 12:53, Mark Foley wrote:
> > > > > > > > On Fri, 10 Nov 2023 17:11:59 Mark Thomas <markt@apache.org
> > > > wrote:
> > > > > > > >>
> > > > > > > >> On 10/11/2023 16:49, Mark Foley wrote:
> > > > > > > >>> I recently upgraded from Tomcat 10.0.17 to 10.1.13.  ...
> > > > > > > >>>
> > > > > > > >>> [deleted]
> > > > > > >
> > > > > > I've put your suggested code in place.
> > > > > >
> > > > > > <%@ page import="jakarta.servlet.http.Part" %>
> > > > > >
> > > > > > I replaced your:
> > > > > >
> > > > > >       throw new IllegalStateException("Expected multi-part");
> > > > > >
> > > > > > with:
> > > > > >
> > > > > >     out.println("Expected multi-part");
> > > > > >
> > > > > > Just to get things compiling OK.  I'll deal with errors later.
> > > > > > With that change, it compiled w/o problem.  I then attempted an
> > > > > > upload.  The
> > > > line:
> > > > > >
> > > > > >    if(null == contentType ||
> > > > > > !contentType.startsWith("multipart/form-data;"))
> > > > > > {
> > > > > >
> > > > > > returned TRUE so it did detect a multipart upload. Yay! That was
> > > > > > a relief However
> > > > > >
> > > > > >    Part fileUpload = request.getPart("param-name");
> > > > > >
> > > > > > Gave me the error:
> > > > > >
> > > > > > java.lang.IllegalStateException: Unable to process parts as no
> > > > > > multi-part configuration has been provided
> > > > > >
> > > > > > So, what does it mean that "no multi-part configuration has been
> > > > provided"?
> > > > > > Is "param-name" something I'm supposed to fill in? I tried
> > > > > > substituting the <input type="file"> field name, "taxResults",
> > > > > > but that
> > > > gave the same error.
> > > > >
> > > > > The form element must have the attribute enctype="multipart/form-
> > > > data".
> > > > > Furthermore, the servlet must be annotated by "@MultipartConfig"
> > > > >
> > > > > I think for jsp files, there is a similar setting in the web.xml.
> > > > > This link might help out:
> > > > > https://stackoverflow.com/questions/37965890/add-annotation-to-jsp
> > > >
> > > > Thanks for your reply Thomas.
> > > >
> > > > I've checked your suggested link and I have no idea where to put all
> > > > that <?xml> stuff.  Furthermore, the poster of that issue didn't say
> > > > he got it working.
> > > >
> > > > A respnder to the post said, "Actually every jsp file will be
> > > > converted to servlet because tomcat can only address servlet and so
> > > > every jsp file is indirectly a servlet and has all the features of
> > > > it". I think he is suggesting that the <?xml> code is superfluous, but not
> > exlpicitly stated as such.
> > > >
> > > > I am getting a TRUE return for
> > > >
> > > >   if(null == contentType ||
> > > > !contentType.startsWith("multipart/form-data;"))
> > > >
> > > > so I think it is recognizing it as "multipart/form-data;".
> > > >
> > > > Does anyone have an example of a JSP program with
> > > > jakarta.servlet.http.Part class?
> > > >
> > > > I'll search for examples as well, but I really have no idea how to proceed.
> > > >
> > > > --Mark F.
> > > >
> > >
> > > The servlet specification defines the special folder WEB-INF.
> > > Within this folder, there is the configuration file named web.xml.
> > > Within this xml-File, the application is configured including the servlets.
> > > JSP-Files are compiled to servlets, either on-the-fly or during compilation
> > time.
> > >
> > > I would recommend to take a look at some sample applications to get
> > familiar with some java web-applications and the web.xml file.
> > >
> > > It is not only about the jsp-file but also the combination with the
> > > application configuration within the web.xml Thus you will need both, jsp-
> > file and a corresponding web.xml configuration.
> > >
> > > Greetings!
> > > Thomas
> > 
> > Thomas, I would LOVE to find some examples on this application! I've spent
> > days searching and mostly I've found examples for pre 10.1 Tomcat, including
> > numerous examples of the type Christopher Schultz found "horrifying".
> > Christopher Schultz's suggested example imported
> > "jakarta.servlet.http.Part", but your link example doesn't mention that class
> > and instead imports "javax.servlet.annotation.MultipartConfig".
> > 
> > As I said, an actual working example somewhere would be nice.
> > 
> > I've used WEB-INF/web.xml for several context definitions, so I am familiar
> > with it, but have never configured a servlet in web.xml.
> > 
> > Anyway, enough griping! I have gotten it partially working thanks to your
> > suggested link, and particulary you suggestion to put the servlet info in
> > web.xml.  I've put the following in WEB-INF/web.xml:
> > 
> > <servlet>
> >   <servlet-name>uploadfile</servlet-name>
> >     <jsp-file>/schDistImportResultsX.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>/schDistImportResultsX.jsp</url-pattern>
> > </servlet-mapping>
> > 
> > I've only changed the <jsp-file> and <url-pattern> tags above. The others are
> > as monkey-typed from your link example. I'll research the other parameters
> > later.
> > 
> > My jsp code is now:
> > 
> > <%@ page import="javax.servlet.annotation.MultipartConfig.*" %>
> > 
> > if((contentType != null) && contentType.startsWith("multipart/form-data;"))
> > {
> >     InputStream inp = null;
> >     DataInputStream ins = null;
> > 
> >     Part fileUpload = request.getPart("taxResults");
> > 
> >     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();
> > 
> > This actually worked!!!! I will experiment with it more and may be back with
> > more questions (e.g. do I really need the web.xml? Could I not do:
> > "inp = fileUpload.getInputStream(mypath);"). But ... maybe later.
> > 
> > Vielen Dank!!! --Mark
> > 
>
> Since Tomcat 10 and later the javax package was moved to Jakarta package.
> So, jakarta.servlet.annotation.MultipartConfig should be the right one.
>
> I don't know why javax works in your case with Tomcat 10.1.
>
> You need the web.xml because you need to annotate the servlet to support Multipart.
>
> Since you have a working example, it should be easier for you to experiment and see when something stops working.
>
> Greetings,
> Thomas

Thomas - I removed my javax.servlet.annotation.MultipartConfig and used
jakarta.servlet.annotation.MultipartConfig per your suggestion. That seemed to
work fine, so I'll keep that if, as you say, Tomcat 10 uses Jakarta. The sample
link you suggested, https://stackoverflow.com/questions/37965890/add-annotation-to-jsp
used Javax which is why I used it.  Even though Javax works, I don't want to get
in trouble in the future!

Thanks --Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


AW: AW: FileUpload class not working with Tomcat 10.1

Posted by "Thomas Hoffmann (Speed4Trade GmbH)" <Th...@speed4trade.com.INVALID>.
Hi Mark!

> -----Ursprüngliche Nachricht-----
> Von: Mark Foley <mf...@novatec-inc.com>
> Gesendet: Dienstag, 14. November 2023 18:20
> An: users@tomcat.apache.org
> Betreff: Re: AW: FileUpload class not working with Tomcat 10.1
> 
> On Tue Nov 14 01:46:09 2023 "Thomas Hoffmann (Speed4Trade GmbH)"
> <Th...@speed4trade.com.INVALID> wrote:
> >
> > Hello Mark,
> >
> > > -----Ursprüngliche Nachricht-----
> > > Von: Mark Foley <mf...@novatec-inc.com>
> > > Gesendet: Montag, 13. November 2023 23:12
> > > An: users@tomcat.apache.org
> > > Betreff: Re: AW: FileUpload class not working with Tomcat 10.1
> > >
> > > On Mon Nov 13 02:18:49 2023 "Thomas Hoffmann (Speed4Trade GmbH)"
> > > <Th...@speed4trade.com.INVALID> wrote:
> > > > Hello,
> > > >
> > > > > -----Ursprüngliche Nachricht-----
> > > > > Von: Mark Foley <mf...@novatec-inc.com>
> > > > > Gesendet: Sonntag, 12. November 2023 19:04
> > > > > An: users@tomcat.apache.org
> > > > > Betreff: Re: FileUpload class not working with Tomcat 10.1
> > > > >
> > > > > On Fri Nov 10 15:57:50 2023 Christopher Schultz
> > > > > <ch...@christopherschultz.net> wrote:
> > > > > >
> > > > > > Mark,
> > > > > >
> > > > > > On 11/10/23 12:53, Mark Foley wrote:
> > > > > > > On Fri, 10 Nov 2023 17:11:59 Mark Thomas <markt@apache.org
> > > wrote:
> > > > > > >>
> > > > > > >> On 10/11/2023 16:49, Mark Foley wrote:
> > > > > > >>> I recently upgraded from Tomcat 10.0.17 to 10.1.13.  ...
> > > > > > >>>
> > > > > > >>> [deleted]
> > > > > >
> > > > > I've put your suggested code in place.
> > > > >
> > > > > <%@ page import="jakarta.servlet.http.Part" %>
> > > > >
> > > > > I replaced your:
> > > > >
> > > > >       throw new IllegalStateException("Expected multi-part");
> > > > >
> > > > > with:
> > > > >
> > > > >     out.println("Expected multi-part");
> > > > >
> > > > > Just to get things compiling OK.  I'll deal with errors later.
> > > > > With that change, it compiled w/o problem.  I then attempted an
> > > > > upload.  The
> > > line:
> > > > >
> > > > >    if(null == contentType ||
> > > > > !contentType.startsWith("multipart/form-data;"))
> > > > > {
> > > > >
> > > > > returned TRUE so it did detect a multipart upload. Yay! That was
> > > > > a relief However
> > > > >
> > > > >    Part fileUpload = request.getPart("param-name");
> > > > >
> > > > > Gave me the error:
> > > > >
> > > > > java.lang.IllegalStateException: Unable to process parts as no
> > > > > multi-part configuration has been provided
> > > > >
> > > > > So, what does it mean that "no multi-part configuration has been
> > > provided"?
> > > > > Is "param-name" something I'm supposed to fill in? I tried
> > > > > substituting the <input type="file"> field name, "taxResults",
> > > > > but that
> > > gave the same error.
> > > >
> > > > The form element must have the attribute enctype="multipart/form-
> > > data".
> > > > Furthermore, the servlet must be annotated by "@MultipartConfig"
> > > >
> > > > I think for jsp files, there is a similar setting in the web.xml.
> > > > This link might help out:
> > > > https://stackoverflow.com/questions/37965890/add-annotation-to-jsp
> > >
> > > Thanks for your reply Thomas.
> > >
> > > I've checked your suggested link and I have no idea where to put all
> > > that <?xml> stuff.  Furthermore, the poster of that issue didn't say
> > > he got it working.
> > >
> > > A respnder to the post said, "Actually every jsp file will be
> > > converted to servlet because tomcat can only address servlet and so
> > > every jsp file is indirectly a servlet and has all the features of
> > > it". I think he is suggesting that the <?xml> code is superfluous, but not
> exlpicitly stated as such.
> > >
> > > I am getting a TRUE return for
> > >
> > >   if(null == contentType ||
> > > !contentType.startsWith("multipart/form-data;"))
> > >
> > > so I think it is recognizing it as "multipart/form-data;".
> > >
> > > Does anyone have an example of a JSP program with
> > > jakarta.servlet.http.Part class?
> > >
> > > I'll search for examples as well, but I really have no idea how to proceed.
> > >
> > > --Mark F.
> > >
> >
> > The servlet specification defines the special folder WEB-INF.
> > Within this folder, there is the configuration file named web.xml.
> > Within this xml-File, the application is configured including the servlets.
> > JSP-Files are compiled to servlets, either on-the-fly or during compilation
> time.
> >
> > I would recommend to take a look at some sample applications to get
> familiar with some java web-applications and the web.xml file.
> >
> > It is not only about the jsp-file but also the combination with the
> > application configuration within the web.xml Thus you will need both, jsp-
> file and a corresponding web.xml configuration.
> >
> > Greetings!
> > Thomas
> 
> Thomas, I would LOVE to find some examples on this application! I've spent
> days searching and mostly I've found examples for pre 10.1 Tomcat, including
> numerous examples of the type Christopher Schultz found "horrifying".
> Christopher Schultz's suggested example imported
> "jakarta.servlet.http.Part", but your link example doesn't mention that class
> and instead imports "javax.servlet.annotation.MultipartConfig".
> 
> As I said, an actual working example somewhere would be nice.
> 
> I've used WEB-INF/web.xml for several context definitions, so I am familiar
> with it, but have never configured a servlet in web.xml.
> 
> Anyway, enough griping! I have gotten it partially working thanks to your
> suggested link, and particulary you suggestion to put the servlet info in
> web.xml.  I've put the following in WEB-INF/web.xml:
> 
> <servlet>
>   <servlet-name>uploadfile</servlet-name>
>     <jsp-file>/schDistImportResultsX.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>/schDistImportResultsX.jsp</url-pattern>
> </servlet-mapping>
> 
> I've only changed the <jsp-file> and <url-pattern> tags above. The others are
> as monkey-typed from your link example. I'll research the other parameters
> later.
> 
> My jsp code is now:
> 
> <%@ page import="javax.servlet.annotation.MultipartConfig.*" %>
> 
> if((contentType != null) && contentType.startsWith("multipart/form-data;"))
> {
>     InputStream inp = null;
>     DataInputStream ins = null;
> 
>     Part fileUpload = request.getPart("taxResults");
> 
>     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();
> 
> This actually worked!!!! I will experiment with it more and may be back with
> more questions (e.g. do I really need the web.xml? Could I not do:
> "inp = fileUpload.getInputStream(mypath);"). But ... maybe later.
> 
> Vielen Dank!!! --Mark
> 

Since Tomcat 10 and later the javax package was moved to Jakarta package.
So, jakarta.servlet.annotation.MultipartConfig should be the right one.

I don't know why javax works in your case with Tomcat 10.1.

You need the web.xml because you need to annotate the servlet to support Multipart.

Since you have a working example, it should be easier for you to experiment and see when something stops working.

Greetings,
Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: AW: FileUpload class not working with Tomcat 10.1

Posted by Mark Foley <mf...@novatec-inc.com>.
On Tue Nov 14 01:46:09 2023 "Thomas Hoffmann (Speed4Trade GmbH)" <Th...@speed4trade.com.INVALID> wrote:
>
> Hello Mark,
>
> > -----Ursprüngliche Nachricht-----
> > Von: Mark Foley <mf...@novatec-inc.com>
> > Gesendet: Montag, 13. November 2023 23:12
> > An: users@tomcat.apache.org
> > Betreff: Re: AW: FileUpload class not working with Tomcat 10.1
> > 
> > On Mon Nov 13 02:18:49 2023 "Thomas Hoffmann (Speed4Trade GmbH)"
> > <Th...@speed4trade.com.INVALID> wrote:
> > > Hello,
> > >
> > > > -----Ursprüngliche Nachricht-----
> > > > Von: Mark Foley <mf...@novatec-inc.com>
> > > > Gesendet: Sonntag, 12. November 2023 19:04
> > > > An: users@tomcat.apache.org
> > > > Betreff: Re: FileUpload class not working with Tomcat 10.1
> > > >
> > > > On Fri Nov 10 15:57:50 2023 Christopher Schultz
> > > > <ch...@christopherschultz.net> wrote:
> > > > >
> > > > > Mark,
> > > > >
> > > > > On 11/10/23 12:53, Mark Foley wrote:
> > > > > > On Fri, 10 Nov 2023 17:11:59 Mark Thomas <markt@apache.org
> > wrote:
> > > > > >>
> > > > > >> On 10/11/2023 16:49, Mark Foley wrote:
> > > > > >>> I recently upgraded from Tomcat 10.0.17 to 10.1.13.  ...
> > > > > >>>
> > > > > >>> [deleted]
> > > > >
> > > > I've put your suggested code in place.
> > > >
> > > > <%@ page import="jakarta.servlet.http.Part" %>
> > > >
> > > > I replaced your:
> > > >
> > > >       throw new IllegalStateException("Expected multi-part");
> > > >
> > > > with:
> > > >
> > > >     out.println("Expected multi-part");
> > > >
> > > > Just to get things compiling OK.  I'll deal with errors later. With
> > > > that change, it compiled w/o problem.  I then attempted an upload.  The
> > line:
> > > >
> > > >    if(null == contentType ||
> > > > !contentType.startsWith("multipart/form-data;"))
> > > > {
> > > >
> > > > returned TRUE so it did detect a multipart upload. Yay! That was a
> > > > relief However
> > > >
> > > >    Part fileUpload = request.getPart("param-name");
> > > >
> > > > Gave me the error:
> > > >
> > > > java.lang.IllegalStateException: Unable to process parts as no
> > > > multi-part configuration has been provided
> > > >
> > > > So, what does it mean that "no multi-part configuration has been
> > provided"?
> > > > Is "param-name" something I'm supposed to fill in? I tried
> > > > substituting the <input type="file"> field name, "taxResults", but that
> > gave the same error.
> > >
> > > The form element must have the attribute enctype="multipart/form-
> > data".
> > > Furthermore, the servlet must be annotated by "@MultipartConfig"
> > >
> > > I think for jsp files, there is a similar setting in the web.xml.
> > > This link might help out:
> > > https://stackoverflow.com/questions/37965890/add-annotation-to-jsp
> > 
> > Thanks for your reply Thomas.
> > 
> > I've checked your suggested link and I have no idea where to put all that
> > <?xml> stuff.  Furthermore, the poster of that issue didn't say he got it
> > working.
> > 
> > A respnder to the post said, "Actually every jsp file will be converted to
> > servlet because tomcat can only address servlet and so every jsp file is
> > indirectly a servlet and has all the features of it". I think he is suggesting that
> > the <?xml> code is superfluous, but not exlpicitly stated as such.
> > 
> > I am getting a TRUE return for
> > 
> >   if(null == contentType || !contentType.startsWith("multipart/form-data;"))
> > 
> > so I think it is recognizing it as "multipart/form-data;".
> > 
> > Does anyone have an example of a JSP program with
> > jakarta.servlet.http.Part class?
> > 
> > I'll search for examples as well, but I really have no idea how to proceed.
> > 
> > --Mark F.
> > 
>
> The servlet specification defines the special folder WEB-INF.
> Within this folder, there is the configuration file named web.xml.
> Within this xml-File, the application is configured including the servlets.
> JSP-Files are compiled to servlets, either on-the-fly or during compilation time.
>
> I would recommend to take a look at some sample applications to get familiar with some java web-applications and the web.xml file.
>
> It is not only about the jsp-file but also the combination with the application configuration within the web.xml
> Thus you will need both, jsp-file and a corresponding web.xml configuration.
>
> Greetings!
> Thomas

Thomas, I would LOVE to find some examples on this application! I've spent days
searching and mostly I've found examples for pre 10.1 Tomcat, including numerous
examples of the type Christopher Schultz found "horrifying". Christopher
Schultz's suggested example imported "jakarta.servlet.http.Part", but your link
example doesn't mention that class and instead imports
"javax.servlet.annotation.MultipartConfig". 

As I said, an actual working example somewhere would be nice.

I've used WEB-INF/web.xml for several context definitions, so I am familiar with
it, but have never configured a servlet in web.xml.

Anyway, enough griping! I have gotten it partially working thanks to your
suggested link, and particulary you suggestion to put the servlet info in
web.xml.  I've put the following in WEB-INF/web.xml:

<servlet>   
  <servlet-name>uploadfile</servlet-name>
    <jsp-file>/schDistImportResultsX.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>/schDistImportResultsX.jsp</url-pattern>
</servlet-mapping>

I've only changed the <jsp-file> and <url-pattern> tags above. The others are
as monkey-typed from your link example. I'll research the other parameters
later. 

My jsp code is now:

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

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

    Part fileUpload = request.getPart("taxResults");

    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();

This actually worked!!!! I will experiment with it more and may be back with
more questions (e.g. do I really need the web.xml? Could I not do: 
"inp = fileUpload.getInputStream(mypath);"). But ... maybe later.

Vielen Dank!!! --Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


AW: AW: FileUpload class not working with Tomcat 10.1

Posted by "Thomas Hoffmann (Speed4Trade GmbH)" <Th...@speed4trade.com.INVALID>.
Hello Mark,


> -----Ursprüngliche Nachricht-----
> Von: Mark Foley <mf...@novatec-inc.com>
> Gesendet: Montag, 13. November 2023 23:12
> An: users@tomcat.apache.org
> Betreff: Re: AW: FileUpload class not working with Tomcat 10.1
> 
> On Mon Nov 13 02:18:49 2023 "Thomas Hoffmann (Speed4Trade GmbH)"
> <Th...@speed4trade.com.INVALID> wrote:
> > Hello,
> >
> > > -----Ursprüngliche Nachricht-----
> > > Von: Mark Foley <mf...@novatec-inc.com>
> > > Gesendet: Sonntag, 12. November 2023 19:04
> > > An: users@tomcat.apache.org
> > > Betreff: Re: FileUpload class not working with Tomcat 10.1
> > >
> > > On Fri Nov 10 15:57:50 2023 Christopher Schultz
> > > <ch...@christopherschultz.net> wrote:
> > > >
> > > > Mark,
> > > >
> > > > On 11/10/23 12:53, Mark Foley wrote:
> > > > > On Fri, 10 Nov 2023 17:11:59 Mark Thomas <markt@apache.org
> wrote:
> > > > >>
> > > > >> On 10/11/2023 16:49, Mark Foley wrote:
> > > > >>> I recently upgraded from Tomcat 10.0.17 to 10.1.13.  ...
> > > > >>>
> > > > >>> [deleted]
> > > >
> > > I've put your suggested code in place.
> > >
> > > <%@ page import="jakarta.servlet.http.Part" %>
> > >
> > > I replaced your:
> > >
> > >       throw new IllegalStateException("Expected multi-part");
> > >
> > > with:
> > >
> > >     out.println("Expected multi-part");
> > >
> > > Just to get things compiling OK.  I'll deal with errors later. With
> > > that change, it compiled w/o problem.  I then attempted an upload.  The
> line:
> > >
> > >    if(null == contentType ||
> > > !contentType.startsWith("multipart/form-data;"))
> > > {
> > >
> > > returned TRUE so it did detect a multipart upload. Yay! That was a
> > > relief However
> > >
> > >    Part fileUpload = request.getPart("param-name");
> > >
> > > Gave me the error:
> > >
> > > java.lang.IllegalStateException: Unable to process parts as no
> > > multi-part configuration has been provided
> > >
> > > So, what does it mean that "no multi-part configuration has been
> provided"?
> > > Is "param-name" something I'm supposed to fill in? I tried
> > > substituting the <input type="file"> field name, "taxResults", but that
> gave the same error.
> >
> > The form element must have the attribute enctype="multipart/form-
> data".
> > Furthermore, the servlet must be annotated by "@MultipartConfig"
> >
> > I think for jsp files, there is a similar setting in the web.xml.
> > This link might help out:
> > https://stackoverflow.com/questions/37965890/add-annotation-to-jsp
> 
> Thanks for your reply Thomas.
> 
> I've checked your suggested link and I have no idea where to put all that
> <?xml> stuff.  Furthermore, the poster of that issue didn't say he got it
> working.
> 
> A respnder to the post said, "Actually every jsp file will be converted to
> servlet because tomcat can only address servlet and so every jsp file is
> indirectly a servlet and has all the features of it". I think he is suggesting that
> the <?xml> code is superfluous, but not exlpicitly stated as such.
> 
> I am getting a TRUE return for
> 
>   if(null == contentType || !contentType.startsWith("multipart/form-data;"))
> 
> so I think it is recognizing it as "multipart/form-data;".
> 
> Does anyone have an example of a JSP program with
> jakarta.servlet.http.Part class?
> 
> I'll search for examples as well, but I really have no idea how to proceed.
> 
> --Mark F.
> 

The servlet specification defines the special folder WEB-INF.
Within this folder, there is the configuration file named web.xml.
Within this xml-File, the application is configured including the servlets.
JSP-Files are compiled to servlets, either on-the-fly or during compilation time.

I would recommend to take a look at some sample applications to get familiar with some java web-applications and the web.xml file.

It is not only about the jsp-file but also the combination with the application configuration within the web.xml
Thus you will need both, jsp-file and a corresponding web.xml configuration.

Greetings!
Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: AW: FileUpload class not working with Tomcat 10.1

Posted by Mark Foley <mf...@novatec-inc.com>.
On Mon Nov 13 02:18:49 2023 "Thomas Hoffmann (Speed4Trade GmbH)" <Th...@speed4trade.com.INVALID> wrote:
> Hello,
>
> > -----Ursprüngliche Nachricht-----
> > Von: Mark Foley <mf...@novatec-inc.com>
> > Gesendet: Sonntag, 12. November 2023 19:04
> > An: users@tomcat.apache.org
> > Betreff: Re: FileUpload class not working with Tomcat 10.1
> > 
> > On Fri Nov 10 15:57:50 2023 Christopher Schultz
> > <ch...@christopherschultz.net> wrote:
> > >
> > > Mark,
> > >
> > > On 11/10/23 12:53, Mark Foley wrote:
> > > > On Fri, 10 Nov 2023 17:11:59 Mark Thomas <markt@apache.org wrote:
> > > >>
> > > >> On 10/11/2023 16:49, Mark Foley wrote:
> > > >>> I recently upgraded from Tomcat 10.0.17 to 10.1.13.  ...
> > > >>>
> > > >>> [deleted]
> > >
> > I've put your suggested code in place. 
> >
> > <%@ page import="jakarta.servlet.http.Part" %>
> >
> > I replaced your:
> > 
> >       throw new IllegalStateException("Expected multi-part");
> > 
> > with:
> > 
> >     out.println("Expected multi-part");
> > 
> > Just to get things compiling OK.  I'll deal with errors later. With that change, it
> > compiled w/o problem.  I then attempted an upload.  The line:
> > 
> >    if(null == contentType || !contentType.startsWith("multipart/form-data;"))
> > {
> > 
> > returned TRUE so it did detect a multipart upload. Yay! That was a relief
> > However
> > 
> >    Part fileUpload = request.getPart("param-name");
> > 
> > Gave me the error:
> > 
> > java.lang.IllegalStateException: Unable to process parts as no multi-part
> > configuration has been provided
> > 
> > So, what does it mean that "no multi-part configuration has been provided"?
> > Is "param-name" something I'm supposed to fill in? I tried substituting the
> > <input type="file"> field name, "taxResults", but that gave the same error.
>
> The form element must have the attribute enctype="multipart/form-data".
> Furthermore, the servlet must be annotated by "@MultipartConfig"
>
> I think for jsp files, there is a similar setting in the web.xml.
> This link might help out: https://stackoverflow.com/questions/37965890/add-annotation-to-jsp 

Thanks for your reply Thomas.

I've checked your suggested link and I have no idea where to put all that <?xml>
stuff.  Furthermore, the poster of that issue didn't say he got it working.

A respnder to the post said, "Actually every jsp file will be converted to
servlet because tomcat can only address servlet and so every jsp file is
indirectly a servlet and has all the features of it". I think he is suggesting
that the <?xml> code is superfluous, but not exlpicitly stated as such.

I am getting a TRUE return for 

  if(null == contentType || !contentType.startsWith("multipart/form-data;"))

so I think it is recognizing it as "multipart/form-data;".

Does anyone have an example of a JSP program with jakarta.servlet.http.Part
class?

I'll search for examples as well, but I really have no idea how to proceed.

--Mark F.

> > >
> > > -chris

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


AW: FileUpload class not working with Tomcat 10.1

Posted by "Thomas Hoffmann (Speed4Trade GmbH)" <Th...@speed4trade.com.INVALID>.
Hello,

> -----Ursprüngliche Nachricht-----
> Von: Mark Foley <mf...@novatec-inc.com>
> Gesendet: Sonntag, 12. November 2023 19:04
> An: users@tomcat.apache.org
> Betreff: Re: FileUpload class not working with Tomcat 10.1
> 
> On Fri Nov 10 15:57:50 2023 Christopher Schultz
> <ch...@christopherschultz.net> wrote:
> >
> > Mark,
> >
> > On 11/10/23 12:53, Mark Foley wrote:
> > > On Fri, 10 Nov 2023 17:11:59 Mark Thomas <markt@apache.org wrote:
> > >>
> > >> On 10/11/2023 16:49, Mark Foley wrote:
> > >>> I recently upgraded from Tomcat 10.0.17 to 10.1.13.  ...
> > >>>
> > >>> [deleted]
> > >>>
> > >>> upgraded to 10.1.13 it is broken again! Here's the error I get:
> > >>>
> > >>> An error occurred at line: [40] in the jsp file:
> > >>> [/schDistImportResults.jsp] The method
> > >>> isMultipartContent(ServletRequestContext) is undefined for the
> > >>> type FileUpload
> > >>
> > >> Tomcat's internal fork of Commons FileUpload isn't intended for
> > >> applications to use. It is not a full fork - just a limited subset
> > >> of the functionality Tomcat needs to implement the Servley upload API.
> > >>
> > >> [deleted]
> > >
> > > My current "basic" implementation is:
> > >
> > > <%@ page import="org.apache.tomcat.util.http.fileupload.*,
> > >      org.apache.tomcat.util.http.fileupload.disk.*,
> > >      org.apache.tomcat.util.http.fileupload.servlet.*,
> > >      org.apache.commons.io.*" %>
> > >
> > > DiskFileItemFactory factory = new DiskFileItemFactory();
> > > ServletFileUpload upload = new ServletFileUpload(factory); List
> > > items = upload.parseRequest(new ServletRequestContext(request));
> > > Iterator iter = items.iterator(); FileItem item = null;
> > >
> > > while (iter.hasNext())
> > > {
> > >      item = (FileItem) iter.next();
> > >
> > >      resultsFile = new File(getServletContext().getRealPath("") +
> > > "/tmp/schTaxResults.txt");
> > >
> > >      try { item.write(resultsFile); }
> > >          catch ( Exception e) { out.println("Exception: " + e); } }
> > >
> > > If you could tell me what the officially prefered Apache Tomcat
> > > FileUpload mechanism is, and what the correct jar and functions are
> > > to accomplish the above, I'd be very grateful!
> >
> >
> > No offense, but the above is horrifying. All that Java code in a JSP
> > makes me cringe. You can do this however you want, but I'd recommend
> > putting Java code into a proper servlet and letting the JSP handle
> > display only.
> >
> > Anyway, I'll get off my soapbox.
> >
> > The easiest thing IMO for you to do is stop trying to parse the upload
> > yourself and use the container. You must have migrated this
> > application forward for like 10 years or something if you are still
> > using a separate library to handle multipart-form-uploads. This has
> > been a part of the code servlet API for some time, now, and you should
> use it:
> 
> This program was originally written 9 years ago and I just monkey-typed the
> original solution from advice and help I found on the web. Likewise, when
> things broke on my upgrade to Tomcat 10.0.17 I got the "how to fix" from
> StackOverflow.
> In short, I've always depended on the kindness of strangers to get this
> Upload mechaism working!
> 
> > import jakarta.servlet.http.Part;
> >
> > ...
> >
> > String contentType = request.getContentType(); if(null == contentType
> > || !contentType.startsWith("multipart/form-data;")) {
> >      logger.warn("Received non-multipart request");
> >
> >      throw new IllegalStateException("Expected multi-part"); }
> >
> > java.io.File tmpDir =
> > (java.io.File)request.getServletContext().getAttribute("javax.servlet.
> > context.tempdir");
> >
> > java.io.File targetFile = new java.io.File(tmpDir,
> > "schTaxResults.txt");
> >
> > Part fileUpload = request.getPart("param-name");
> >
> > if(null != fileUpload) {
> >      fileUpload.write(targetFile.getAbsolutePath());
> > }
> 
> I've removed my "horrifying" code and put your suggested code in place. I
> replaced your:
> 
>       throw new IllegalStateException("Expected multi-part");
> 
> with:
> 
>     out.println("Expected multi-part");
> 
> Just to get things compiling OK.  I'll deal with errors later. With that change, it
> compiled w/o problem.  I then attempted an upload.  The line:
> 
>    if(null == contentType || !contentType.startsWith("multipart/form-data;"))
> {
> 
> returned TRUE so it did detect a multipart upload. Yay! That was a relief
> However
> 
>    Part fileUpload = request.getPart("param-name");
> 
> Gave me the error:
> 
> java.lang.IllegalStateException: Unable to process parts as no multi-part
> configuration has been provided
> 
> So, what does it mean that "no multi-part configuration has been provided"?
> Is "param-name" something I'm supposed to fill in? I tried substituting the
> <input type="file"> field name, "taxResults", but that gave the same error.


The form element must have the attribute enctype="multipart/form-data".
Furthermore, the servlet must be annotated by "@MultipartConfig"

I think for jsp files, there is a similar setting in the web.xml.
This link might help out: https://stackoverflow.com/questions/37965890/add-annotation-to-jsp 


> > I have made some obvious and not-so-obvious changes, here. First, you
> > don't need a separate library: you are relying on the container for
> > the multi-part handling.
> >
> > Second, I have changed from uploading the file directly into the
> > servlet context (the live running application~) into a temporary
> > directory. If you want to serve this file back out to clients, you may
> > want to use WebDAV or some other protocol rather than file-upload, or
> maybe not.
> 
> I may revisit this after I get your basic solution working. This particular app can
> upload more than one file, but first things first.
> 
> > If you want to serve this file back to clients, I *highly* recommend
> > creating a directory OUTSIDE your web application where you can push
> > uploaded files, and then use something like <PostResources> to allow
> > Tomcat to load content from that location, mounted on a path that
> > won't allow users to upload binaries, etc. that might get loaded by
> > the application.
> 
> In this case, this same app reads the "uploaded" file and updates a database,
> but thanks for this tip.
> 
> > You may want to be careful about how you are writing. If two requests
> > come-in at the same time, thee files may overwrite each other in
> > unpredictable ways.
> >
> > -chris
> 
> Thanks --Mark F.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: FileUpload class not working with Tomcat 10.1

Posted by Mark Foley <mf...@novatec-inc.com>.
On Fri Nov 10 15:57:50 2023 Christopher Schultz <ch...@christopherschultz.net> wrote:
>
> Mark,
>
> On 11/10/23 12:53, Mark Foley wrote:
> > On Fri, 10 Nov 2023 17:11:59 Mark Thomas <markt@apache.org wrote:
> >>
> >> On 10/11/2023 16:49, Mark Foley wrote:
> >>> I recently upgraded from Tomcat 10.0.17 to 10.1.13.  ...
> >>> 
> >>> [deleted]
> >>>
> >>> upgraded to 10.1.13 it is broken again! Here's the error I get:
> >>>
> >>> An error occurred at line: [40] in the jsp file: [/schDistImportResults.jsp]
> >>> The method isMultipartContent(ServletRequestContext) is undefined for the type FileUpload
> >>
> >> Tomcat's internal fork of Commons FileUpload isn't intended for
> >> applications to use. It is not a full fork - just a limited subset of
> >> the functionality Tomcat needs to implement the Servley upload API.
> >>
> >> [deleted]
> > 
> > My current "basic" implementation is:
> > 
> > <%@ page import="org.apache.tomcat.util.http.fileupload.*,
> >      org.apache.tomcat.util.http.fileupload.disk.*,
> >      org.apache.tomcat.util.http.fileupload.servlet.*,
> >      org.apache.commons.io.*" %>
> > 
> > DiskFileItemFactory factory = new DiskFileItemFactory();
> > ServletFileUpload upload = new ServletFileUpload(factory);
> > List items = upload.parseRequest(new ServletRequestContext(request));
> > Iterator iter = items.iterator();
> > FileItem item = null;
> > 
> > while (iter.hasNext())
> > {
> >      item = (FileItem) iter.next();
> > 
> >      resultsFile = new File(getServletContext().getRealPath("") + "/tmp/schTaxResults.txt");
> > 
> >      try { item.write(resultsFile); }
> >          catch ( Exception e) { out.println("Exception: " + e); }
> > }
> > 
> > If you could tell me what the officially prefered Apache Tomcat FileUpload
> > mechanism is, and what the correct jar and functions are to accomplish the above, I'd be
> > very grateful!
>
>
> No offense, but the above is horrifying. All that Java code in a JSP 
> makes me cringe. You can do this however you want, but I'd recommend 
> putting Java code into a proper servlet and letting the JSP handle 
> display only.
>
> Anyway, I'll get off my soapbox.
>
> The easiest thing IMO for you to do is stop trying to parse the upload 
> yourself and use the container. You must have migrated this application 
> forward for like 10 years or something if you are still using a separate 
> library to handle multipart-form-uploads. This has been a part of the 
> code servlet API for some time, now, and you should use it:

This program was originally written 9 years ago and I just monkey-typed the
original solution from advice and help I found on the web. Likewise, when things
broke on my upgrade to Tomcat 10.0.17 I got the "how to fix" from StackOverflow.
In short, I've always depended on the kindness of strangers to get this Upload 
mechaism working!

> import jakarta.servlet.http.Part;
>
> ...
>
> String contentType = request.getContentType();
> if(null == contentType || !contentType.startsWith("multipart/form-data;")) {
>      logger.warn("Received non-multipart request");
>
>      throw new IllegalStateException("Expected multi-part");
> }
>
> java.io.File tmpDir = 
> (java.io.File)request.getServletContext().getAttribute("javax.servlet.context.tempdir");
>
> java.io.File targetFile = new java.io.File(tmpDir, "schTaxResults.txt");
>
> Part fileUpload = request.getPart("param-name");
>
> if(null != fileUpload) {
>      fileUpload.write(targetFile.getAbsolutePath());
> }

I've removed my "horrifying" code and put your suggested code in place. I
replaced your:

      throw new IllegalStateException("Expected multi-part");

with:

    out.println("Expected multi-part");

Just to get things compiling OK.  I'll deal with errors later. With that change, it
compiled w/o problem.  I then attempted an upload.  The line:

   if(null == contentType || !contentType.startsWith("multipart/form-data;")) {

returned TRUE so it did detect a multipart upload. Yay! That was a relief However

   Part fileUpload = request.getPart("param-name");

Gave me the error:

java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided

So, what does it mean that "no multi-part configuration has been provided"? Is
"param-name" something I'm supposed to fill in? I tried substituting the 
<input type="file"> field name, "taxResults", but that gave the same error.

> I have made some obvious and not-so-obvious changes, here. First, you 
> don't need a separate library: you are relying on the container for the 
> multi-part handling.
>
> Second, I have changed from uploading the file directly into the servlet 
> context (the live running application~) into a temporary directory. If 
> you want to serve this file back out to clients, you may want to use 
> WebDAV or some other protocol rather than file-upload, or maybe not.

I may revisit this after I get your basic solution working. This particular app
can upload more than one file, but first things first.

> If you want to serve this file back to clients, I *highly* recommend 
> creating a directory OUTSIDE your web application where you can push 
> uploaded files, and then use something like <PostResources> to allow 
> Tomcat to load content from that location, mounted on a path that won't 
> allow users to upload binaries, etc. that might get loaded by the 
> application.

In this case, this same app reads the "uploaded" file and updates a database,
but thanks for this tip.

> You may want to be careful about how you are writing. If two requests 
> come-in at the same time, thee files may overwrite each other in 
> unpredictable ways.
>
> -chris

Thanks --Mark F.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: FileUpload class not working with Tomcat 10.1

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Mark,

On 11/10/23 12:53, Mark Foley wrote:
> On Fri, 10 Nov 2023 17:11:59 Mark Thomas <markt@apache.org wrote:
>>
>> On 10/11/2023 16:49, Mark Foley wrote:
>>> I recently upgraded 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
>>> thanks to postings on stackoverflow, but now that I've
>>> upgraded to 10.1.13 it is broken again! Here's the error I get:
>>>
>>> An error occurred at line: [40] in the jsp file: [/schDistImportResults.jsp]
>>> The method isMultipartContent(ServletRequestContext) is undefined for the type FileUpload
>>
>> Tomcat's internal fork of Commons FileUpload isn't intended for
>> applications to use. It is not a full fork - just a limited subset of
>> the functionality Tomcat needs to implement the Servley upload API.
>>
>> If you want to use Commons File Upload, add the JAR to your web
>> application and use it.
>>
>> Alternatively, if you want to use the Servlet upload API then use that.
>>
>> If the javax.sevlet -> jakarta.servlet transition means you can't use
>> your preferred version of Commons File Upload in Tomcat 10.1.x (very
>> likely) then run your preferred version of Commons File Upload through
>> Tomcat's migration tool for Jakarta EE and use the converted version of
>> Commons File Upload in your web application.
>>
>> Depending on Tomcat internals is very likely to lead to breakage.
>>
>> Mark
> 
> Thanks for your quick reply. Whatever I've been using keeps breaking. I had it
> working in 9.0.14 and earlier, then it broke with 10.0.17 and I fixed that, now
> it's broken again with 10.1.13. So, my "prefered" solution is whatever is
> recommended and is likely to continue to be supported without breaking in future
> Tomcats.
> 
> What do you recommend? And do you have a quickie template somewhere which shows
> the basic include(s) and method I need? I really don't do anything very fancy.
> My current "basic" implementation is:
> 
> <%@ page import="org.apache.tomcat.util.http.fileupload.*,
>      org.apache.tomcat.util.http.fileupload.disk.*,
>      org.apache.tomcat.util.http.fileupload.servlet.*,
>      org.apache.commons.io.*" %>
> 
> DiskFileItemFactory factory = new DiskFileItemFactory();
> ServletFileUpload upload = new ServletFileUpload(factory);
> List items = upload.parseRequest(new ServletRequestContext(request));
> Iterator iter = items.iterator();
> FileItem item = null;
> 
> while (iter.hasNext())
> {
>      item = (FileItem) iter.next();
> 
>      resultsFile = new File(getServletContext().getRealPath("") + "/tmp/schTaxResults.txt");
> 
>      try { item.write(resultsFile); }
>          catch ( Exception e) { out.println("Exception: " + e); }
> }
> 
> If you could tell me what the officially prefered Apache Tomcat FileUpload
> mechanism is, and what the correct jar and functions are to accomplish the above, I'd be
> very grateful!


No offense, but the above is horrifying. All that Java code in a JSP 
makes me cringe. You can do this however you want, but I'd recommend 
putting Java code into a proper servlet and letting the JSP handle 
display only.

Anyway, I'll get off my soapbox.

The easiest thing IMO for you to do is stop trying to parse the upload 
yourself and use the container. You must have migrated this application 
forward for like 10 years or something if you are still using a separate 
library to handle multipart-form-uploads. This has been a part of the 
code servlet API for some time, now, and you should use it:

import jakarta.servlet.http.Part;

...

String contentType = request.getContentType();
if(null == contentType || !contentType.startsWith("multipart/form-data;")) {
     logger.warn("Received non-multipart request");

     throw new IllegalStateException("Expected multi-part");
}

java.io.File tmpDir = 
(java.io.File)request.getServletContext().getAttribute("javax.servlet.context.tempdir");

java.io.File targetFile = new java.io.File(tmpDir, "schTaxResults.txt");

Part fileUpload = request.getPart("param-name");

if(null != fileUpload) {
     fileUpload.write(targetFile.getAbsolutePath());
}

I have made some obvious and not-so-obvious changes, here. First, you 
don't need a separate library: you are relying on the container for the 
multi-part handling.

Second, I have changed from uploading the file directly into the servlet 
context (the live running application~) into a temporary directory. If 
you want to serve this file back out to clients, you may want to use 
WebDAV or some other protocol rather than file-upload, or maybe not.

If you want to serve this file back to clients, I *highly* recommend 
creating a directory OUTSIDE your web application where you can push 
uploaded files, and then use something like <PostResources> to allow 
Tomcat to load content from that location, mounted on a path that won't 
allow users to upload binaries, etc. that might get loaded by the 
application.

You may want to be careful about how you are writing. If two requests 
come-in at the same time, thee files may overwrite each other in 
unpredictable ways.

-chris

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: FileUpload class not working with Tomcat 10.1

Posted by Mark Foley <mf...@novatec-inc.com>.
On Fri, 10 Nov 2023 17:11:59 Mark Thomas <markt@apache.org wrote:
>
> On 10/11/2023 16:49, Mark Foley wrote:
> > I recently upgraded 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
> > thanks to postings on stackoverflow, but now that I've
> > upgraded to 10.1.13 it is broken again! Here's the error I get:
> > 
> > An error occurred at line: [40] in the jsp file: [/schDistImportResults.jsp]
> > The method isMultipartContent(ServletRequestContext) is undefined for the type FileUpload
>
> Tomcat's internal fork of Commons FileUpload isn't intended for 
> applications to use. It is not a full fork - just a limited subset of 
> the functionality Tomcat needs to implement the Servley upload API.
>
> If you want to use Commons File Upload, add the JAR to your web 
> application and use it.
>
> Alternatively, if you want to use the Servlet upload API then use that.
>
> If the javax.sevlet -> jakarta.servlet transition means you can't use 
> your preferred version of Commons File Upload in Tomcat 10.1.x (very 
> likely) then run your preferred version of Commons File Upload through 
> Tomcat's migration tool for Jakarta EE and use the converted version of 
> Commons File Upload in your web application.
>
> Depending on Tomcat internals is very likely to lead to breakage.
>
> Mark

Thanks for your quick reply. Whatever I've been using keeps breaking. I had it
working in 9.0.14 and earlier, then it broke with 10.0.17 and I fixed that, now
it's broken again with 10.1.13. So, my "prefered" solution is whatever is
recommended and is likely to continue to be supported without breaking in future
Tomcats.

What do you recommend? And do you have a quickie template somewhere which shows
the basic include(s) and method I need? I really don't do anything very fancy.
My current "basic" implementation is:

<%@ page import="org.apache.tomcat.util.http.fileupload.*,
    org.apache.tomcat.util.http.fileupload.disk.*,
    org.apache.tomcat.util.http.fileupload.servlet.*,
    org.apache.commons.io.*" %>

DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(new ServletRequestContext(request));
Iterator iter = items.iterator();
FileItem item = null;

while (iter.hasNext())
{
    item = (FileItem) iter.next();

    resultsFile = new File(getServletContext().getRealPath("") + "/tmp/schTaxResults.txt");

    try { item.write(resultsFile); }
        catch ( Exception e) { out.println("Exception: " + e); }
}    

If you could tell me what the officially prefered Apache Tomcat FileUpload
mechanism is, and what the correct jar and functions are to accomplish the above, I'd be
very grateful!

Thanks --Mark

> > [deleted]

> > 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:
> > [deleted]
> > 
> > I have quite a few programs that use the FileUpload methods. Does anyone know how to fix this latest breakage?
> > 
> > Thanks --Mark
> > 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: FileUpload class not working with Tomcat 10.1

Posted by Mark Thomas <ma...@apache.org>.
On 10/11/2023 16:49, Mark Foley wrote:
> I recently upgraded 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
> thanks to postings on stackoverflow, but now that I've
> upgraded to 10.1.13 it is broken again! Here's the error I get:
> 
> An error occurred at line: [40] in the jsp file: [/schDistImportResults.jsp]
> The method isMultipartContent(ServletRequestContext) is undefined for the type FileUpload

Tomcat's internal fork of Commons FileUpload isn't intended for 
applications to use. It is not a full fork - just a limited subset of 
the functionality Tomcat needs to implement the Servley upload API.

If you want to use Commons File Upload, add the JAR to your web 
application and use it.

Alternatively, if you want to use the Servlet upload API then use that.

If the javax.sevlet -> jakarta.servlet transition means you can't use 
your preferred version of Commons File Upload in Tomcat 10.1.x (very 
likely) then run your preferred version of Commons File Upload through 
Tomcat's migration tool for Jakarta EE and use the converted version of 
Commons File Upload in your web application.

Depending on Tomcat internals is very likely to lead to breakage.

Mark

> 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
> 
> 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
> 
> I have quite a few programs that use the FileUpload methods. Does anyone know how to fix this latest breakage?
> 
> Thanks --Mark
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org