Home > General > FileUploadServlet

FileUploadServlet

November 17, 2011


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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.rapidhealth.dao.RapidHealthDAO;
import com.rapidhealth.pojo.DiscoveryConfig;
import com.rapidhealth.scp.SCPDispatcher;
import com.rapidhealth.utils.ApplicationContextHolder;

public class FileUploadServlet extends HttpServlet
{
private final Logger log = LoggerFactory.getLogger(FileUploadServlet.class);

private static final String TMP_DIR_PATH = "/tmp";
private static final String DESTINATION_DIR_PATH = "/repository";

private File tmpDir;
private File destinationDir;
private String realPath;

public void init(ServletConfig config) throws ServletException
{
super.init(config);
tmpDir = new File(TMP_DIR_PATH);
if (!tmpDir.isDirectory())
{
throw new ServletException(TMP_DIR_PATH + " is not a directory");
}
realPath = getServletContext().getRealPath(DESTINATION_DIR_PATH);
destinationDir = new File(realPath);
if (!destinationDir.isDirectory())
{
throw new ServletException(DESTINATION_DIR_PATH + " is not a directory");
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String fileName = null;

PrintWriter out = response.getWriter();
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
/*
* Set the size threshold, above which content will be stored on disk.
*/
fileItemFactory.setSizeThreshold(1 * 1024 * 1024); // 1 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();
DiscoveryConfig config = new DiscoveryConfig();
while (itr.hasNext())
{
FileItem fi = (FileItem) itr.next();
/*
* Handle Form Fields.
*/
if ( fi.isFormField() )
{
System.out.println("File Name = " + fi.getFieldName() + ", Value = " + fi.getString());
if(fi.getFieldName().equals("sIp"))
{
config.setStartIp(fi.getString());
}else if(fi.getFieldName().equals("eIp"))
{
config.setEndIp(fi.getString());
}else if(fi.getFieldName().equals("un"))
{
config.setUserName(fi.getString());
}else if(fi.getFieldName().equals("pwd"))
{
config.setPwd(fi.getString());
}else if(fi.getFieldName().equals("dd"))
{
config.setDestFileDirLoc(fi.getString());
}
}
else
{
fileName = fi.getName();

config.setFileName(fileName);
config.setFileLoc(realPath+File.separatorChar+fileName);
config.setCreatedDateTime(System.currentTimeMillis());

File file = new File(destinationDir, fi.getName());
fi.write(file);
}
}
log.info("File {} uploaded successfully at location {}",fileName,destinationDir);

RapidHealthDAO healthDAO = (RapidHealthDAO)ApplicationContextHolder.getAppContext().getBean("rapidDAO");
healthDAO.saveorupdate(config);

log.debug("Configuration Saved to DB Successfully");

SCPDispatcher dispatcher = (SCPDispatcher)ApplicationContextHolder.getAppContext().getBean("scpPool");
dispatcher.startupload(config);
log.debug("File {} started uploading",config.getFileName());

out.print("File upload started successfully");
}
catch (Exception ex)
{
log("Error encountered while parsing the request", ex);

out.print("Failed to save data");
}
out.close();
}
}
Advertisements
Categories: General
%d bloggers like this: