加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用swfUpload实现现文件上传

发布时间:2020-12-15 17:29:47 所属栏目:百科 来源:网络整理
导读:首先下载http://commons.apache.org/proper/commons-fileupload/?, 推荐用这个 FileUpload 1.2.2 - 29 July 2010 在这里里任选一个包,这里下载的少了一个commons-io-2.2.jar这个jar包,请在这里下载http://commons.apache.org/proper/commons-io/download_

首先下载http://commons.apache.org/proper/commons-fileupload/?,

推荐用这个 FileUpload 1.2.2 - 29 July 2010

在这里里任选一个包,这里下载的少了一个commons-io-2.2.jar这个jar包,请在这里下载http://commons.apache.org/proper/commons-io/download_io.cgi

?

或者直接用struts2里面的两个jar包(分别是commons-fileupload-1.3.1.jar?? 和 commons-io-2.2.jar),stuts2里面用的是

FileUpload 1.3.1 - 7 February 2014里面的这个commons-fileupload-1.3.1.jar??包

?

第二,在eclipse或者myeclipse里面新建一个Web工程(在eclipse里面一定要功态Web工程),拷贝上面下载的两个jar包,也就是commons-fileupload-1.3.1.jar?? 和 commons-io-2.2.jar到web工具的? lib? 路径下。

直接新建一个jsp或者html文件。复制下面的文件上传表单代码过去。代码清单如下:

<!DOCTYPE >
<html>
<meta  charset="utf-8">
<head>
<title>SWFUpload</title>
<link href="js/swfupload/css/default.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/swfupload//js/swfupload.js"></script>
<script type="text/javascript" src="js/swfupload//js/swfupload.queue.js"></script>
<script type="text/javascript" src="js/swfupload//js/fileprogress.js"></script>
<script type="text/javascript" src="js/swfupload//js/handlers.js"></script>
<script type="text/javascript">
		var swfu;

		window.onload = function() {
			var settings = {
				flash_url : "js/swfupload/swf/swfupload.swf",upload_url: <a target=_blank href="http://localhost:8080/goods/upload.jsp">http://localhost:8080/goods/upload.jsp</a>,//这里写上你的文件上传的处理页	
				post_params: {"PHPSESSID" : ""},file_size_limit : "100 MB",file_types : "*.*",file_types_description : "All Files",file_upload_limit : 10,//配置上传个数
				file_queue_limit : 0,file_post_name : 'Filedata',custom_settings : {
					progressTarget : "fsUploadProgress",cancelButtonId : "btnCancel"
				},debug: true,// Button settings
				button_image_url: "js/swfupload/images/TestImageNoText_65x29.png",button_width: "65",button_height: "29",button_placeholder_id: "spanButtonPlaceHolder",button_text: '<span class="theFont">浏览</span>',button_text_style: ".theFont { font-size: 16; }",button_text_left_padding: 12,button_text_top_padding: 3,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	
			};

			swfu = new SWFUpload(settings);
	     };
	</script>
</head>
<body>
<div id="header">
	<h1 id="logo"><a href="index.htm">SWFUpload</a></h1>
	<div id="version">v2.2.0</div>
</div>

<div id="content">
	<form id="form1" action="" method="post" enctype="multipart/form-data">
		<p>点击“浏览”按钮,选择您要上传的文档文件后,系统将自动上传并在完成后提示您。</p>
		<p>请勿上传包含中文文件名的文件!</p>
		<div class="fieldset flash" id="fsUploadProgress">
			<span class="legend">快速上传</span>
	  </div>
		<div id="divStatus">0 个文件已上传</div>
			<div>
				<span id="spanButtonPlaceHolder"></span>
				<input id="btnCancel" type="button" value="取消所有上传" onclick="swfu.cancelQueue();" disabled="disabled" style="margin-left: 2px; font-size: 8pt; height: 29px;">
			</div>

	</form>
</div>

</body>
</html>


文件处理jsp的代码清单如下:

<%@page import="java.io.*"%>
<%@page import="java.text.*"%>
<%@page import="java.util.*"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	FileItemFactory itemFactory = new DiskFileItemFactory();
	ServletFileUpload upload = new ServletFileUpload(itemFactory);

	List<FileItem> items = upload.parseRequest(request);
	if (items != null) {
		for (FileItem item : items) {
			String name = item.getName();
				/* 
				* 这里不用这个了哦
				* if (name != null) { 
				*/  
				if(item.isFormField()){  
	                  continue;  
	              }else{
				System.out.println("文件名:" + name);
				System.out.println("文件:" + item.getFieldName());
				System.out.println("文件名大小: " + item.getSize() + " B");
				System.out.println("文件名类型: " + item.getContentType());

				String root = request.getSession().getServletContext()
						.getRealPath("/");
				System.out.println("当前上传到服务器的根目录是 :   " + root);

				String separator = java.io.File.separator;//得到当前系统的文件分隔符

				String path = root + "upload";
				System.out.println("当前上传到服务器的path目录是 :   " + path);

				/**
				创建一个保存上传文件的文件夹
				 */
				File file = new File(path);
				if (!file.exists()) {
					file.mkdirs();
				}

				
				//以当前精确到秒的日期为上传的文件的文件名  
				SimpleDateFormat sdf = new SimpleDateFormat(
						"yyyyMMddkkmmss");
				String type = item.getName().split(".")[1];//获取文件类型  
				File savedFile = new File(path,sdf.format(new Date())
						+ "." + type);
				item.write(savedFile);
			}
		}

	}
%>


?

?

?

至此,实现文件上传的过程全部搞完了。总结点:? 在eclipse中一定要进行文件重命名,不然得到的都是一个文件夹,而不是得到文件内容。

说明一些内容:

判断某项是否是普通的表单类型。
if (name != null) {    //jsp里面的这个代码见面说明。。推荐用这里的这种说明,也就是写上jsp代码那样子的形式。
例如:在上传文件时用到
List<FileItem> list = upload.parseRequest(request);
for(FileItem item : list){
   if(item.isFormField()){
        。。。。//判断该表单项是否是普通类型
   }else{
       。。。。 //否则该表单项是file 类型的
   }
}

这个案例的全部文件下载地址是:http://url.cn/dQNKA6

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读