• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Issue with commons-fileupload2

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I downloaded the zip file for jar, but there is no jakarta jar file in it.
this gives error, I tried using but then again I am not able to use
I need to parse HttpServletRequest object using commons-fileupload2, thanks in advance.
 
MyExamCloud Software Support
Posts: 753
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After receiving an HTTP request, the request can be parsed only once. This is because the client has only sent the request once and it has already been consumed in its entirety during the first parse. Any subsequent attempts to parse the same request will not be successful as the request is no longer available.

In order to parse the request more than once, the client would need to send the request multiple times. However, this is not a practical solution as it is not feasible to ask or expect a client to do so.

One possible solution is to parse the request once and then use the parsed information for multiple purposes. For example, if the request contains file data and form fields, the request can be parsed once and then the file data and form fields can be processed separately using the same parsed information.

 
Farhan Shaik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, thanks for this but it still does not work
DiskFileItemFactory() is now private I guess, it says not visible. I am not able to use it.
and also ServletFileUpload is from commons.fileupload, I am using fileupload2, and parserequest expects javax.servlet.http.HttpServletRequest, I am using jakarta
 
Sheriff
Posts: 4642
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch  

It would be easier to understand your issue and make some suggestions if you could share some code which demonstrates the problem that you are facing.
 
Farhan Shaik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am using jsp form where there is some form data and file uploads,


and in the other jsp I call this methods,

I need to parse this request in the method so I can do new ProductForm(title,...) , I am using tomcat10.
But tomcat10 uses jakarta, I tried new DiskFileItemFactory which gives a lot of errors first it asks for params and when i give params it says not visible. Is there any other way to parse this? and also when I downloaded the jar file there was no commons.fileupload2.jakarta, only core javax and portlet as some of the sites show to use this commons.fileupload2.jakarta
 
Ron McLeod
Sheriff
Posts: 4642
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any Java code which is using Apache FileUpload.
 
Farhan Shaik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I was trying to do this, if there is any other way to parse even that is fine. I got this from apache docs
This code has issues with DiskFileItemFactory and JakartaServletDiskFileUpload, DiskFileItemFactory this seems to be private and the other is not available.
 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been watching this thread, but I didn't understand it, so I waited to see if someone else did.

Welcome to the Ranch, Farhan!

I think you have a misunderstanding on how file uploads work.

When you upload files using HTTP POST, you're not doing a DOS-style file copy from one machine to another. Instead, the client builds an HTTP data stream and the data that's IN the file(s) is copied into the data stream along with the HTTP headers and other protocol data.

The server receives all that data as a unit. The file data isn't required to ever be stored in an actual file on the server.  Depending on the server and its configuration, the "file" may just be something in server RAM.

The Apache API is responsible for making the file data accessible to the web application, and if it's like most such file uploads, what you'll actually be doing is obtaining a an InputStream that you can read from. You can take the data read from that stream and store it in a file, push it into a database as a BLOB, simply eat it to create your response stream, or do whatever you want to with it.

Now one thing you DON'T want to do is store that data in Tomcat's temporary file directory. Unless you really truly want that file to be temporary. Otherwise you may look there some day and it will be deleted.

Another place you don't want to create/update a file is in your webapp's WAR directory OR within the Tomcat server itself. That's another way to lose data.

Under Unix/Linux, the common place to store uploaded data would be in an application-specific sub-directory under /var/lib. On Windows, there is no convention, so choose whatever location is convenient for you.
 
Farhan Shaik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim Holloway I need to parse multipart/form-data to separate files and form fields, I would need to implement a multipart parser and manually it seems very complex, I just started learning java. I was hoping to use a library instead. I just need help with getting this data into separate variables.

This was the actual code
 
Ron McLeod
Sheriff
Posts: 4642
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have a Servlets environment handy to try a proper solution, but it should work something like this (focus on the Fileupload2 bits and ignore the Microprofile stuff):
I hope this helps.
 
Farhan Shaik
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ron McLeod DiskFileItemFactory shows no error, thanks so much but JakartaServletFileUpload and JakartaServletRequestContext these are not available, there is no fileuploads2-jakarta jar file present in the official zip file, where as in github I can see this but .java file.
Tim Holloway also I have tried using inputstream which works perfectly, but I still want to see why JakartaServletFileUpload and JakartaServletRequestContext are not available as https://commons.apache.org/proper/commons-fileupload/using.html it says here to use it.
 
Ron McLeod
Sheriff
Posts: 4642
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used the org.apache.commons:commons-fileupload2:2.0.0-M2 maven dependency, which pulled in both the core and jakarta jars.

I'm not sure where to download the M2 jar from, the the M1 jar can be found here: commons-fileupload2-jakarta-2.0.0-M1.jar


Correction: I used the org.apache.commons:commons-fileupload2:2.0.0-M1 dependency
 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. Fair warning. Be very careful about filenames in file uploads. Some clients (I'm looking at YOU, Internet Explorer!) would ship the client's absolute filename path. Others would simply supply a relative path. In any case, consider the path part as suspect as it could be used for malicious purposes.

Farhan, you might find this construct to be useful:

Notice that since this form of File constructor handles the joining of path and filename internally, you don't have to worry about what the pathname separator was or if it was properly present. Code is a bit simpler and it's more "write-once/run-anywhere" as the same code works unchanged under Linux, Windows and MacOS. Only the uploadPath needs adjusting based on the OS being used.
 
Farhan Shaik
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is my current code which is working perfectly now, thanks Ron McLeod and  Time Holloway
 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah!, Ah!, Ah!  

Remember what I said about never uploading files to the inside of the WAR???
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic