package com.cv.servlet.pdf.upload;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
/**
* Servlet implementation class PdfFileUploadHandler
*
* @author Chandra Vardhan
*
*/
public class PdfFileUploadHandler extends HttpServlet {
private static final Logger LOGGER = Logger.getLogger(PdfFileUploadHandler.class);
private static String storagePath = null;
/**
* @see HttpServlet#HttpServlet()
*/
public PdfFileUploadHandler() {
super();
}
@Override
public void init() throws ServletException {
// process only if its multipart content
File file = new File("C:\\pdf_files");
if (!file.exists()) {
file.mkdir();
storagePath = file.getPath()+"\\";
LOGGER.info("LOGGER : Folder has been created successfully : " + storagePath);
} else {
storagePath = file.getPath()+"\\";
LOGGER.info("LOGGER : Folder already exist : " + storagePath);
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
LOGGER.info(
"LOGGER : Entered into doGet(HttpServletRequest ,HttpServletResponse ) of PdfFileUploadHandler class... ");
PrintWriter out = response.getWriter();
response.setContentType("text/html");
if (ServletFileUpload.isMultipartContent(request)) {
try {
List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : multiparts) {
if (!item.isFormField()) {
String name = new File(item.getName()).getName();
LOGGER.error("File name is : : "+ name);
// This line is used to write the uploaded file to
// working folder
item.write(new File(storagePath + name));
}
}
// File uploaded successfully
} catch (Exception ex) {
LOGGER.error("File Upload Failed due to " + ex);
out.print("<br><br><font color='RED'><center>File Upload Failed... </center></font>");
}
} else {
LOGGER.error("Sorry this Servlet only handles file upload request");
out.print("<br><br><font color='RED'><center>Sorry this Servlet only handles file upload request</center></font>");
}
out.print("<br><br><font color='GREEN'><center>File Uploaded Successfully @ : " + storagePath+"</center></font>");
LOGGER.info("LOGGER : File Uploaded Successfully @ : " + storagePath);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
LOGGER.info(
"LOGGER : Entered into doPost(HttpServletRequest ,HttpServletResponse ) of PdfFileUploadHandler class... ");
doGet(request, response);
}
} |
No comments:
Post a Comment