Sunday 13 November 2011

How do you upload a file to your web application? -From book java/j2ee interview

Upload file via web application then the default encoding and GET methods are not suitable for file upload and a form containing file input fields must specify the encoding type “multipart/formdata”and the POST method in the <form>tag as shown below:
When the user clicks the “Upload” button, the client browser locates the local file and sends it to the server using HTTP POST. When it reaches your server, your implementing servlet should process the POST data in order to extract the encoded file. Unfortunately, application servers implementing the Servlet and JSP specifications are not required to handle the multipart/form-data encoding. Fortunately there are number of libraries available such
as Apache Commons File Upload, which is a small Java package that lets you obtain the content of the uploaded file from the encoded form data. The API of this package is flexible enough to keep small files in memory while large files are stored on disk in a “temp” directory. You can specify a size threshold to determine when to keep in memory and when to write to disk.

No comments:

Post a Comment