aspose file tools*
The moose likes Beginning Java and the fly likes Uploading a file from a website, then passing the file to a model through a controller Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Uploading a file from a website, then passing the file to a model through a controller" Watch "Uploading a file from a website, then passing the file to a model through a controller" New topic
Author

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

Matthew Busse
Ranch Hand

Joined: Sep 29, 2010
Posts: 52
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
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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/


Steve
Matthew Busse
Ranch Hand

Joined: Sep 29, 2010
Posts: 52
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Sep 29, 2010
Posts: 52
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Uploading a file from a website, then passing the file to a model through a controller