Win a copy of Functional Design and Architecture this week in the Functional programming forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Upload a file to Java Web App running in Tomcat behind Apache

 
Ranch Hand
Posts: 150
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a web appliction running in Tomcat (using Struts2). The Tomcat is running behind the Apache server.

I have a  use case where I have to upload a file. The functionality is working fine if I access the application directly on Tomcat (IP address:8080). But when I access the application through Apache, the uploaded file is not available to tomcat.

Not sure where I have to fix this.


Thanks in Advance.
Bala.
 
Saloon Keeper
Posts: 7089
165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does "not available" mean? What steps have you taken to debug this - is the code doing logging before, during and after the file upload process?
 
Bala Tilak
Ranch Hand
Posts: 150
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Tim. Yes I am logging.

I mean the File object is null when I access the application through Apache. All the other request parameters are ok.. only the File object is null.

 
Saloon Keeper
Posts: 24283
167
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. You've got me confused. When you upload via a JEE webapp server such as Tomcat, you don't get a "File Object". You have a variable in the HTTP POST sent to Tomcat (via Apache or not) that corresponds to the file upload HTTP control (<input type="file")  on the client's web form. The receiving servlet can then use getPart to obtain a reference to an object that can return the user-selected upload filename and the file contents, >

HTTP is not a file server. When you "upload/download" a file via HTTP, you don't actually do filesystem operations. Instead the data contained within the file in question is copied as a MIME component over the HTTP Request or Response stream.

So again, there is no file. To store the uploaded data on your webserver's filesystem, you have to obtain it as a data stream using the Part's getInputStream() method and then create a local File and write to it. Or, if your needs demand it, output it to a BLOB in a database. Or just scan the data stream and pull out the parts that interest you for further processing.

For the official tutorial, I recommend https://docs.oracle.com/javaee/6/tutorial/doc/glraq.html
 
Tim Moores
Saloon Keeper
Posts: 7089
165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post the relevant code excerpts that handle the file upload.
 
Tim Holloway
Saloon Keeper
Posts: 24283
167
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 do need to qualify that for Struts.

The underlyng file upload mechanism for HTTP is as I've described it. However, Struts does copy the uploaded data stream into a temporary File if you use Struts FileUploadAction. You would then have to figure out what to do with that temporary file, since in many cases in Jaava, temporary files can be set to delete themselves automatically. Raw HTTP file uploads don't actually ever have to live in a file - if the webapp server wants to, it can keep the data stream in RAM, but Struts itself can make a file out of it.

Docs on how all Struts FileUpload Action works are here: https://struts.apache.org/core-developers/file-upload.html#struts-21x-file-upload-dependencies

If no file object was produced, check the webapp logs for errors.
 
Bala Tilak
Ranch Hand
Posts: 150
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the confusion I created.

The file upload working fine with the Tomcat server. I am getting the fileUpload object if I tested it on my local system right localhost:8080/mywebapp/uploadfile

or Even its successful If I directly access the Tomcat via 8080 on server like https://myipaddress:8080/mywebapp/uploadfile.

But Its not working if I access the application via Apache 4 , that is is Apache 4 --> Tomcat 9 --> Struts. Means when I access the application like https://myipaddress:80/uploadfile the fileUpload is null.


 
Tim Moores
Saloon Keeper
Posts: 7089
165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume you left out the method that would set the "fileUpload" variable - is it being called, with a null parameter?
 
Bala Tilak
Ranch Hand
Posts: 150
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. I have setter and getter for FileUpload. I restarted the Tomcat and Apache and it's started working now. Not sure how it was fixed.

Thanks a lot for your time and thought Tim.
 
reply
    Bookmark Topic Watch Topic
  • New Topic