下面是效果介绍:
项目目录截图:
代码介绍
1、Index.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>
<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;
window.onload = function() {
upload1 = new SWFUpload({
//提交路径
upload_url: "upload.action",
//向后台传递额外的参数
post_params: {"name" : "kaobian"},
//上传文件的名称
file_post_name: "file",
// 下面自己按照字面意思理解
file_size_limit : "102400", // 100MB
file_types : "*.*",
file_types_description : "All Files",
file_upload_limit : "10",
file_queue_limit : "0",
// 事件处理
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_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
});
}
</script>
</head>
<body>
</div>
<div id="content">
<form action="upload.action" method="post" name="thisform" enctype="multipart/form-data">
<table>
<tr valign="top">
<td>
<div>
<div style="padding-left: 5px;">
<span id="spanButtonPlaceholder1"></span>
<!--<input type="button" value="上传" onclick="upload1.addPostParam('idname',encodeURI(document.getElementById('myFileName').value));upload1.startUpload();"/>
--><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 class="fieldset flash" id="fsUploadProgress1">
<span class="legend">文件上传</span>
</div>
</div>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
Upload.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.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private File file;
private String fileFileName;
private String fileContentType;
private String savePath;
public String execute() throws Exception {
InputStream is = new FileInputStream(file);
String root = getSavePath();
//String tempName = System.currentTimeMillis()+this.getFileFileName().substring(this.getFileFileName().indexOf("."));
File deskFile = new File(root, this.getFileFileName());
OutputStream os = new FileOutputStream(deskFile);
byte[] bytefer = new byte[1024];
int length = 0;
while ((length = is.read(bytefer)) != -1) {
os.write(bytefer, 0, length);
}
os.close();
is.close();
return "success";
}
public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
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;
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 配置struts2.1.8 上传是文件的最大限制为100M -->
<constant name="struts.multipart.maxSize" value="104857600" />
<!-- 设置struts2 上传文件时 保存的临时目录 -->
<constant name="struts.multipart.saveDir" value="C:\temp"></constant>
<package name="struts2" extends="struts-default">
<action name="upload" class="com.action.FileUploadAction">
<param name="savePath">/upload</param>
<result name="success">/index.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
官方站点:
http://www.swfupload.org/
DEMO地址:
http://demo.swfupload.org/
SWFUplaod学习笔记阅读顺序
一、了解SWFUpload
http://hanxin0311.iteye.com/blog/1915611
二、详细介绍SWFUpload
http://hanxin0311.iteye.com/blog/1915615
三、struts2+swfUpload简单例子
http://hanxin0311.iteye.com/blog/1915626
四、struts2+swfUpload深度整合
http://hanxin0311.iteye.com/blog/1915628
SWFUpload像服务器传递参数
http://hanxin0311.iteye.com/blog/1913946
SWFUpload接受服务器Action返回的参数
http://hanxin0311.iteye.com/blog/1915644
SWFUpload中文乱码问题
http://hanxin0311.iteye.com/blog/1915648
附件提供代码下载
项目开发工具 myeclipse8.5
下载代码 (如果附件不能下载点击此处下载)
- 大小: 4.8 KB
- 大小: 24 KB
- 大小: 26.1 KB
分享到:
相关推荐
在这个"四、struts2+swfUpload例子二"中,我们将深入探讨如何将这两者结合,以实现高效的文件上传功能。 首先,为了集成Struts2和SwfUpload,我们需要在项目中引入相应的依赖库。对于Struts2,这通常包括struts2-...
本教程将详细讲解如何利用SWFUpload与Struts2框架来实现多文件上传的示例。 首先,让我们了解SWFUpload。SWFUpload是一款开源的Flash上传组件,它允许用户在不离开当前页面的情况下上传多个文件。通过在浏览器上...
SWFUpload上传组件的一个完整的例子,后台由Struts2完成,有上传进度显示,可以多文件上传,如果你想使用SWFUpload组件但还不知道怎样使用,可以下载下来运行一下试试,看一下源码就知道了。
SWFUpload上传组件的一个完整的例子,后台由Struts2完成,有上传进度显示,可以多文件上传,如果你想使用SWFUpload组件但还不知道怎样使用,可以下载下来运行一下试试,看一下源码就知道了。
利用swfupload结合struts2实现的一个上传mp3和mp4文件,并可以在页面上用html5进行播放,本例只是自己动手写的一个小例子。注意:为了节省资源,例子中并未包含struts2的jar,需要手动添加struts2用到的jar包
在服务器端,SWFUpload支持多种编程语言,本例中使用的是Java(Struts 1.x)。 #### 目录结构解析 SWFUpload的核心文件夹包含了所有必需的资源和脚本,包括但不限于: - **SWFUpload**:核心文件夹,包含SWFUpload...
总之,这个例子为理解SWFUpload与Java的整合提供了一个基础示例,展示了如何构建一个简单的文件上传功能。实际项目中,我们需要考虑代码的组织、性能优化、安全性以及兼容性等问题,以实现一个健壮且用户友好的文件...