Pr.Pg Next Pg

File Uploading using Servlet tutorials

  • HTML form tag allow user to upload files to the server. Uploading file could be anything from text file to binary file.

  • Form tag with Post method send to servlet for File Uploading to server.

 

Creating a File Upload Form

  • To create a File Upload form in html you will use form tags method attribute as POST method GET method can not be used because of file size and binary data files, you will also use enctype attribute as multipart/form-data.

  • The form action attributes should be set to servlet file which would handles file uploading process, we will use uploading servlet file in this example to upload file.

  • 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, we also need one <input../> tag with attribute typle=”submit” to send the file to servlet for further processing and uploading.

 

  • Writing Backend Servlet

  • Below is the uploading servlet which would take care of accepting uploaded file and to store it in root directory’s uploads folder of your application.

  • Note you will need commons-fileupload.jar and commons-io.jar file for performing the upload operation in servlets, please click here to download commons-fileupload.jar and for downloading commons-io.jar click here.

 

Example: Servlet program to upload a file

Sam Sir

//Program name uploading.java

// Program to upload a file

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Iterator;

import java.util.List;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.apache.commons.fileupload.FileItem;

import org.apache.commons.fileupload.FileItemFactory;

import org.apache.commons.fileupload.FileUploadException;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

 

public class uploading extends HttpServlet {

 

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

PrintWriter out = response.getWriter();

boolean isMultipart = ServletFileUpload.isMultipartContent(request);

 

if (isMultipart) {

// Create a factory for disk-based file items

FileItemFactory factory = new DiskFileItemFactory();

 

// Create a new file upload handler

ServletFileUpload upload = new ServletFileUpload(factory);

 

try {

// Parse the request

List /* FileItem */ items = upload.parseRequest(request);

Iterator iterator = items.iterator();

while (iterator.hasNext()) {

FileItem item = (FileItem) iterator.next();

if (!item.isFormField()) {

String fileName = item.getName();

String root = getServletContext().getRealPath("/");

File path = new File(root + "/uploads");

if (!path.exists()) {

boolean status = path.mkdirs();

}

 

File uploadedFile = new File(path + "/" + fileName);

System.out.println(uploadedFile.getAbsolutePath());

item.write(uploadedFile);

out.println("File Uploaded Successfully at "+uploadedFile.getAbsolutePath());

}

}

} catch (FileUploadException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

}

 

}

 

 

 

 

 

HTML form for file browsing which you want to upload

Sam Sir

<html>

<body>

<h1>File Upload Form</h1>

<fieldset>

<legend>Upload File</legend>

<form action="uploading" method="post" enctype="multipart/form-data">

<label for="fileName">Select File: </label>

<input id="fileName" type="file" name="fileName" size="30"/><br/>

<input type="submit" value="Upload"/>

</form>

</fieldset>

</body>

</html>

Output

Uploading file using servlets

 

 

Uploading files using servlets

Pr.Pg border                                              Next Pg