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 + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>
<link href="css/swfupload.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="js/SWFUpload/swfupload.js"></script>
<script type="text/javascript" src="js/SWFUpload/swfupload.queue.js"></script>
<script type="text/javascript" src="js/SWFUpload/fileprogress.js"></script>
<script type="text/javascript" src="js/SWFUpload/handlers.js"></script>

<script type="text/javascript">
		var swfu;

		window.onload = function() {
			var settings = {
				flash_url : "js/SWFUpload/swfupload.swf",
				upload_url: "upload.do",
				file_size_limit : "200 MB",
				file_types : "*.csv",
				file_types_description : "All Files",
				file_upload_limit : 100,
				file_queue_limit : 0,
				custom_settings : {
					progressTarget : "fsUploadProgress",
					cancelButtonId : "btnCancel"
				},
				debug: false,

				// Button settings
				button_image_url: "images/TestImageNoText_65x29.png",
				button_width: "135",
				button_height: "29",
				button_placeholder_id: "spanButtonPlaceHolder",
				button_text: '<span class="theFont">Files</span>',
				button_text_style: ".theFont { font-size: 16; }",
				button_text_left_padding: 12,
				button_text_top_padding: 3,
				
				// The event handler functions are defined in handlers.js
				file_queued_handler : fileQueued,
				file_queue_error_handler : fileQueueError,
				file_dialog_complete_handler : fileDialogComplete,
				upload_start_handler : uploadStart,
				upload_progress_handler : uploadProgress,
				upload_error_handler : uploadError,
				upload_success_handler : uploadSuccess,
				upload_complete_handler : uploadComplete,
				queue_complete_handler : queueComplete	// Queue plugin event
			};

			swfu = new SWFUpload(settings);
	     };
	</script>


<body style="font-size: 12px;">

<div id="content">
	<h2>Upload files</h2>
	<form id="form1" action="upload.do" method="post" enctype="multipart/form-data">

			<div class="fieldset flash" id="fsUploadProgress">
			<span class="legend">Upload Queue</span>
			</div>
		<div id="divStatus">0 Files Uploaded</div>
			<div>
				<span id="spanButtonPlaceHolder"></span>
				<input id="btnCancel" type="button" value="Cancel All Uploads" onclick="swfu.cancelQueue();" disabled="disabled" style="margin-left: 2px; font-size: 8pt; height: 29px;" />
			</div>

	</form>
</div>
<br/>
<a href="filesList.do">Files List</a><br/>

</body>
</html>

 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<FileItem> 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:

<dependency>
		<groupId>commons-fileupload</groupId>
		<artifactId>commons-fileupload</artifactId>
		<version>1.3</version>
</dependency>

 

你可能感兴趣的:(swfupload)