• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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

 
Ranch Hand
Posts: 143
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: 6888
163
  • 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: 143
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: 23409
159
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: 6888
163
  • 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: 23409
159
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.
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic