swfuplaod 结合common-fileupload servlet 上传文件

阅读更多

其实 swfupload 就是一个前端上传控件,和后端没什么联系,唯一的联系就是 提交url 和 servlet 返回200,告诉你上传成功了

jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>






My JSP 'index.jsp' starting page



















Upload files

Upload Queue
0 Files Uploaded

Files List

 servlet:

public class UploadFileServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp) {
		File fileList=null;
		try {
			
			//先判断存储文件的路径在不在,如果不存在,则创建该目录
			File dir=new File(Config.getUploadFilesDir());
			if(!dir.exists()){
				boolean b=dir.mkdirs();
			}
			
			//如果存储文件的“DB”不存在,则创建
			fileList=new File(Config.getFileNamesDB());
			if(!fileList.exists()){
				fileList.createNewFile();
			}

			DiskFileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload=new ServletFileUpload(factory);
			List items=upload.parseRequest(request);
//			System.out.println("items.size()===>"+items.size());
//			System.out.println("fileName1===>"+items.get(0).getName());
//			System.out.println("fileName2===>"+items.get(1).getName());
//			System.out.println("fileName3===>"+items.get(2).getName());
			
			Iterator iterator=items.iterator();
			while(iterator.hasNext()){
				FileItem item=(FileItem)iterator.next();
					if(!item.isFormField()){
						item.write(new File(Config.getUploadFilesDir()+item.getName()));
						TxtTools.appendContent(Config.getFileNamesDB(), item.getName());
						PrintWriter out=resp.getWriter();
						out.print("200");//告诉swfupload上传成功了,swfuplaod默认 收到200 为成功,其它返回请具体查看swfupload 的api
						out.close();
					}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			//
		}
	
		
	}

}

 pom:


		commons-fileupload
		commons-fileupload
		1.3

 

你可能感兴趣的:(swfupload)