• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Uploading a file from a website, then passing the file to a model through a controller

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

I'm trying to write a servlet that takes a file uploaded by a user from a website, and then passes that file to another java class that's going to do lots of fun stuff with it.

All the explanations I could find for handling uploaded files talked about using the FileUpload libraries from Apache Commons. However, using that, I wind up with FileItems, not files. My java program takes a File as an argument. I tried to cast the FileItem to a File, but then I get this error from Tomcat when I tried to submit the form:



Here's my servlet:



All I want to do is have the servlet pass the file to the java model class. The examples I found online of creating a List of FileItems seems incredibly complicated, is there an easier way I can do this?

Thanks!

Matt
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The FileItem from the Apache API is a Data Transfer Object, more or less. It provides access to the bytes of data to be stored in the file. To get an actual file on disk, you need to write those bytes to the disk into a File. The API will help you with that, as will the example code. Once the data in the FileItem is written to disk then you can create a java File object to reference it.

To see more about how to use the Apache FileUpload API read about it here: http://commons.apache.org/fileupload/
 
Matthew Busse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:The FileItem from the Apache API is a Data Transfer Object, more or less. It provides access to the bytes of data to be stored in the file. To get an actual file on disk, you need to write those bytes to the disk into a File. The API will help you with that, as will the example code. Once the data in the FileItem is written to disk then you can create a java File object to reference it.

To see more about how to use the Apache FileUpload API read about it here: http://commons.apache.org/fileupload/



I see, thank you. I understand doing what you describe if I want the servlet to manipulate the bytes of data stored in the file. I'm wondering if there is a way to upload the file as a whole and just pass the whole file to the other java program. I can do something similar with a GUI, just create a file chooser that selects a file, then passes the file to the model.

Is this situation completely different because the file is being transmitted across a network?

I greatly appreciate your help!

Matt
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is completely different, because the original file is on the client computer - which from the server's point of view could be anywhere. The only way to get that file is to transmit it to the Server. The Server receives the file as bytes, and needs to know what to do with it. In Java, a 'File' is just an representation for a storage point on a disk. When you transfer it from one class to another you are just passing around the 'address' of the File on disk. Since that address and disk make no sense when communicating between unrelated computers, then you need to transfer the data itself - not just the address.

So the Web App will ask the user for the file. The client submits the file's contents, and the FileUpload API helps you take those contents and do stuff with it. Since your current code requires a Java File, then what you need to do is create a physical file on disk which you can then pass around the address for. If your pre-written code allowed you to pass in an InputStream them you could pass around direct access to the data contents, rather than forcing the data to a file on disk first. But from your description that isn't possible with the code you currently have.
 
Matthew Busse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:Yes, it is completely different, because the original file is on the client computer - which from the server's point of view could be anywhere. The only way to get that file is to transmit it to the Server. The Server receives the file as bytes, and needs to know what to do with it. In Java, a 'File' is just an representation for a storage point on a disk. When you transfer it from one class to another you are just passing around the 'address' of the File on disk. Since that address and disk make no sense when communicating between unrelated computers, then you need to transfer the data itself - not just the address.

So the Web App will ask the user for the file. The client submits the file's contents, and the FileUpload API helps you take those contents and do stuff with it. Since your current code requires a Java File, then what you need to do is create a physical file on disk which you can then pass around the address for. If your pre-written code allowed you to pass in an InputStream them you could pass around direct access to the data contents, rather than forcing the data to a file on disk first. But from your description that isn't possible with the code you currently have.



Thank you for that detailed explanation! It makes much more sense to me now.

I could possibly rewrite the code in the model to accept an InputStream, but it sounds like it might be easier to just write the file to a disk, that way I don't have to change the code I already have working.

Thanks!

Matt
 
reply
    Bookmark Topic Watch Topic
  • New Topic