Servlet Tutorials

Servlets - File Uploading
Previous Home Next
adplus-dvertising

In Servlet can be used with an HTML form tag and it allows users to upload files to the server.Servlet can be uploaded file could be a text file or image file or any document.

Using the HTML code to create an uploader form. Some important points are as follows:

  1. The form method attribute should be set to POST method and GET method can not be used.
  2. The form enctype attribute should be set to multipart/form-data.
  3. The form action attribute should be set to a servlet file which would handle file uploading at backend server. Following example is using UploadServlet servlet to upload file.
  4. To upload a single file you should use a single <input .../ >tag with attribute type="file". To allow multiple files uploading, include more than one input tags with different values for the name attribute. The browser associates a Browse button with each of them.

Example

<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="UploadServlet" method="post"
                        enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
To display following result which would allow to select a file from local PC and when user would click at "Upload File", form would be submitted along with the selected file:

File Upload: Select a file to upload:

Previous Home Next