upload code

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());
	    }



	}




}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值