Thursday, 12 December 2013

Creating directory dynamically on server and uploading files into it

                   In one of the J2EE requirement I worked on, had to upload some files from client and store them on serverin a specific location. The solution had to be implementd in simple servlet without any framework, so came up with following simple soultion that was implemented using apache commons library and simple servlet. Thought 

                    The code below uploads the file to a folder "upload" under the current application in the server ,for simplicity I used this location. The same can be changed to any location needed as per requirement.

This requires commons-fileupload.jar in project classpath.

import java.io.File; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Iterator; 
import java.util.List; 
import javax.servlet.ServletConfig; 
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.FileUploadException; 
import org.apache.commons.fileupload.disk.DiskFileItemFac tory; 
import org.apache.commons.fileupload.servlet.ServletFileU pload; 

public class UploadServlet extends HttpServlet 
/** 
*/ 
private static final long serialVersionUID = 1L; 

private static final String TMP_DIR_PATH = "/temp"; 
private File tmpDir; 
private static final String DESTINATION_DIR_PATH ="/upload"; //Folder under your server where uploaded file will be saved. 
private File destinationDir; 

public void init(ServletConfig config) throws ServletException 
super.init(config); 
String realPath = getServletContext().getRealPath(""); 

//System.out.println("Real path where file will be saved : "+realPath); 

tmpDir = new File(realPath+TMP_DIR_PATH); 
if(!tmpDir.exists()) 
tmpDir.mkdir(); 

destinationDir = new File(realPath+DESTINATION_DIR_PATH); 
if(!destinationDir.exists()) 
destinationDir.mkdir(); 

@SuppressWarnings("unchecked") 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
PrintWriter out = response.getWriter(); 
response.setContentType("text/plain"); 

DiskFileItemFactory fileItemFactory = new DiskFileItemFactory (); 
/* 
*Set the size threshold, above which content will be stored on disk. 
*/ 
fileItemFactory.setSizeThreshold(2*1024*1024); //Maximum file size which can be uploaded to server = 2 MB 
/* 
* Set the temporary directory to store the uploaded files of size above threshold. 
*/ 
fileItemFactory.setRepository(tmpDir); 

ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory); 
try { 
/* 
* Parse the request 
*/ 
List items = uploadHandler.parseRequest(request); 
Iterator itr = items.iterator(); 
while(itr.hasNext()) 
FileItem item = (FileItem) itr.next(); 
/* 
* Handle Form Fields. 
*/ 
if(item.isFormField()) 
out.println("File Name = "+item.getFieldName()+", Value = "+item.getString()); 
else 
//Handle Uploaded files. 
out.println(" 
<h1>File uploaded successfully. Following are the information.</h1> 
"); 
out.println(); 
out.println("File Name = "+item.getName()+"\nContent type = "+item.getContentType()+"\nFile Size = "+item.getSize()+"\nUploaded Location = "+destinationDir.getPath()); 
/* 
* Write file to the ultimate location. 
*/ 
File file = new File(destinationDir,item.getName()); 
item.write(file); 
out.close(); 
}catch(FileUploadException ex) 
log("Error encountered while parsing the request",ex); 
catch(Exception ex) 
log("Error encountered while uploading file",ex); 

This solution can be improved in many ways.

2 comments: