是用SmartUpload进行文件上传

阅读更多
package com.jamesby.file.servlet;

public class UpLoadServlet extends HttpServlet {
	private static Log log = LogFactory.getLog(UpLoadServlet.class);
	
	private ServletConfig config;
	
	final public void init(ServletConfig config) throws ServletException {
		this.config = config;
	}
	
	final public ServletConfig getServletConfig() {
		return config;
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws
	ServletException, IOException {
		doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response) throws
	ServletException, IOException {
		try {
			SmartUpload su = new SmartUpload();
			
			su.initialize(config, request, response);
			
			su.setMaxFileSize(1024 * 1024 * 4); //4M
			
			su.upload();
			
			String paramValue = su.getRequest().getParameter("paramName");
			
			String path = "d:/upload";
			for (int i = 0; i < su.getFiles().getCount(); i++) {
				
				com.jspsmart.upload.File file = su.getFiles().getFile(i);
				
				String fileName = file.getFileName();
				
				if (!file.isMissing())  file.saveAs(path + "/"+fileName,SmartUpload.SAVE_PHYSICAL);
				else continue;
			}
			return null;
		} catch (Exception ex) {
		
		}
	}
}


你可能感兴趣的:(Servlet)