package org.uploadtag.servlet;
import java.io.IOException;
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.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.uploadtag.commonsfileupload.MonitoredDiskFileItem;
import org.uploadtag.commonsfileupload.MonitoredDiskFileItemFactory;
import org.uploadtag.commonsfileupload.UploadListener;
import org.uploadtag.service.impl.UploadServiceImpl;
import org.uploadtag.sessionobjects.UploadError;
public class UploadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("UploadServlet:doGet");
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("UploadServlet:doPost");
String areaID = request.getParameter("areaID");
String attachmentID = request.getParameter("attachmentID");
System.out.println("UploadServlet:doPost: attachmentID = " + attachmentID);
String maxSizeString = request.getParameter("maxSize");
int maxSize = 0;
if ((maxSizeString!=null)&&(!maxSizeString.equals(""))) {
maxSize = Integer.parseInt(maxSizeString);
}
System.out.println("UploadServlet:doPost: maxSizeString = " + maxSizeString);
System.out.println("UploadServlet:doPost: maxSize = " + maxSize);
UploadServiceImpl service = new UploadServiceImpl(request.getSession());
UploadListener listener = new UploadListener(request, service, areaID, attachmentID,30);
service.cleanUploadErrorStatus(areaID,attachmentID);
// Create a factory for disk-based file items
MonitoredDiskFileItemFactory factory = new MonitoredDiskFileItemFactory(listener);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
if (maxSize>0) {
upload.setSizeMax(maxSize*1024);
}
try
{
// process uploads ..
upload.parseRequest(request);
}
catch (FileUploadBase.SizeLimitExceededException e) {
e.printStackTrace();
//new ExceptionUtil().logMessageForException(e);
//new ExceptionUtil().sendMessageForException(e);
service.updateUploadErrorStatus(areaID,attachmentID,UploadError.FILE_SIZE_LIMIT);
}
catch (FileUploadException e)
{
e.printStackTrace();
//new ExceptionUtil().logMessageForException(e);
//new ExceptionUtil().sendMessageForException(e);
}
List fileItemList = factory.getFileItems();
service.saveFileItemsListInSession(areaID, attachmentID,fileItemList);
System.out.println("UploadServlet:doPost: itemList = " + fileItemList);
System.out.println("UploadServlet:doPost: itemList.size() = " + fileItemList.size());
for (int i=0;i<fileItemList.size();i++) {
MonitoredDiskFileItem item = (MonitoredDiskFileItem)fileItemList.get(i);
System.out.println("UploadServlet:doPost: item = " + item);
System.out.println("UploadServlet:doPost: item.getName() = " + item.getName());
}
}
}