`
qqybh
  • 浏览: 66657 次
  • 性别: Icon_minigender_1
  • 来自: 太原
文章分类
社区版块
存档分类
最新评论

swfupload+Struts2多文件上传

阅读更多
   公司做了一个相册模块,其中有一个要用多文件上传,可是使用的是很老的单文件上传的模式,感觉使用不是很方便,于是在网上搜索了一下,搜到了swfupload这个上传组件,于是尝试着使用struts2整合swfupload写了写,现在把代码放上来。
一、起始页代码
<%@ 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>
  <link href="<%=basePath%>css/default.css" rel="stylesheet" type="text/css" />
   	<script type="text/javascript" src="<%=basePath%>js/swfupload.js"></script>
   	<script type="text/javascript" src="<%=basePath%>js/swfupload.queue.js"></script>
    <script type="text/javascript" src="<%=basePath%>js/fileprogress.js"></script>
    <script type="text/javascript" src="<%=basePath%>js/handlers.js"></script>
   	<!-- 初始化swfupload 对象-->
   <script type="text/javascript">
		var upload1, upload2;

		window.onload = function() {
			upload1 = new SWFUpload({
				// Backend Settings
				upload_url: "PictureAction.action",
				post_params: {"picSESSID" : "songhao"},
				file_post_name: "file",
				// File Upload Settings
				file_size_limit : "102400",	// 100MB
				file_types : "*.*",
				file_types_description : "All Files",
				file_upload_limit : "10",
				file_queue_limit : "0",

				// Event Handler Settings (all my handlers are in the Handler.js file)
				file_dialog_start_handler : fileDialogStart,
				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,

				// Button Settings
				button_image_url : "images/XPButtonUploadText_61x22.png",
				button_placeholder_id : "spanButtonPlaceholder1",
				button_width: 61,
				button_height: 22,
				
				// Flash Settings
				flash_url : "js/swfupload.swf",
				

				custom_settings : {
					progressTarget : "fsUploadProgress1",
					cancelButtonId : "btnCancel1"
				},
				
				// Debug Settings
				debug: false
			});
		
			upload2 = new SWFUpload({
				// Backend Settings
				upload_url: "PictureAction.action",
				post_params: {"SESSID" : "file"},

				// File Upload Settings
				file_size_limit : "200",	// 200 kb
				file_types : "*.jpg;*.gif;*.png",
				file_types_description : "Image Files",
				file_upload_limit : "10",
				file_queue_limit : "5",

				// Event Handler Settings (all my handlers are in the Handler.js file)
				file_dialog_start_handler : fileDialogStart,
				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,

				// Button Settings
				button_image_url : "images/XPButtonUploadText_61x22.png",
				button_placeholder_id : "spanButtonPlaceholder2",
				button_width: 61,
				button_height: 22,
				
				// Flash Settings
				flash_url : "js/swfupload.swf",

				swfupload_element_id : "flashUI2",		// Setting from graceful degradation plugin
				degraded_element_id : "degradedUI2",	// Setting from graceful degradation plugin

				custom_settings : {
					progressTarget : "fsUploadProgress2",
					cancelButtonId : "btnCancel2"
				},

				// Debug Settings
				debug: false
			});

	     }
	    
	</script>
  </head>
  
  <body>
  <div id="header">
	<h1 id="logo"><a href="../">SWFUpload</a></h1>
	<div id="version">v2.2.0</div>
</div>
  <div id="content">
	<h2>Multi-Instance Demo</h2>
    <form action="pictureAction" method="post" name="thisform" enctype="multipart/form-data">
    	<p>This page demonstrates how multiple instances of SWFUpload can be loaded on the same page.
			It also demonstrates the use of the graceful degradation plugin and the queue plugin.</p>
		<table>
			<tr valign="top">
				<td>
					<div>
						<div class="fieldset flash" id="fsUploadProgress1">
							<span class="legend">Large File Upload Site</span>
						</div>
						<div style="padding-left: 5px;">
							<span id="spanButtonPlaceholder1"></span>
							<input id="btnCancel1" type="button" value="Cancel Uploads" onclick="cancelQueue(upload1);" disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />
							<br />
						</div>
					</div>
				</td>
				<td>
					<div>
						<div class="fieldset flash" id="fsUploadProgress2">
							<span class="legend">Small File Upload Site</span>
						</div>
						<div style="padding-left: 5px;">
							<span id="spanButtonPlaceholder2"></span>
							<input id="btnCancel2" type="button" value="Cancel Uploads" onclick="cancelQueue(upload2);" disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />
							<br />
						</div>
					</div>
				</td>
			</tr>
		</table>
    </form>
    </div>
  </body>
</html>

二、upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>
   
  </head>
  
  <body>
        <s:form action="PictureAction.action" enctype="multipart/form-data">
        	<s:file name="file"></s:file>
        	<s:submit></s:submit>
        </s:form>
  </body>
</html>

三、action文件
package com.action;



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts2.ServletActionContext;


import com.model.Picture;
import com.opensymphony.xwork2.ActionSupport;

public class PictureAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private File file;
	
    private String fileFileName;
    
    private String fileContentType;
    
    
	
	public File getFile() {
		return file;
	}



	public void setFile(File file) {
		this.file = file;
	}



	public String getFileFileName() {
		return fileFileName;
	}



	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}



	public String getFileContentType() {
		return fileContentType;
	}



	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}



	/**
	 * 用于实现上传功能
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		//实现上传
		InputStream is = new FileInputStream(file);
		String root = ServletActionContext.getRequest().getRealPath("/upload");
		System.out.println(this.getFileContentType() );
		File deskFile = new File(root,this.getFileFileName());
		OutputStream os = new FileOutputStream(deskFile);
		byte [] bytefer = new byte[400];
		int length = 0 ; 
		while((length = is.read(bytefer) )>0)
		{
			os.write(bytefer,0,length);
		}
		os.close();
		is.close();
		return SUCCESS;
	}
	 
		
}


上传不上去的原因的是没有新建一个文件夹 upload
分享到:
评论
9 楼 ahhmdwg 2014-11-29  
inde.jsp的是多文件上传,看错了不好意思!  谢谢
8 楼 ahhmdwg 2014-11-29  
这哪是多文件上传啊,upload.jsp就一个文件上传,坑你爹啊
7 楼 13776349315 2014-05-28  
很棒,值得学习,在根目录新建一个upload文件夹就能上传成功了,不过queue有限制不知道在哪里控制,我再看看~谢谢LZ的分享
6 楼 bobommmm 2013-10-28  
这个upload文件夹是应该建在哪里呢?
5 楼 zhuange 2013-10-21  
4 楼 qqybh 2012-12-14  
个重action返回的是什么东西? 如果后台出现什么情况
q412239930 写道
你这个重action返回的是什么东西? 如果后台出现什么情况没有上传成功,怎么样返回数据到前台?

这个只是一个主体代码,action返回的SUCCESS是struts2的基本配置而已。。这个没有异常处理
3 楼 q412239930 2012-06-11  
你这个重action返回的是什么东西? 如果后台出现什么情况没有上传成功,怎么样返回数据到前台?
2 楼 luger 2012-05-15  
    
1 楼 ZoeXin 2010-11-10  

相关推荐

Global site tag (gtag.js) - Google Analytics