三、struts2+swfUpload简单例子

下面是效果介绍:
[img]http://dl2.iteye.com/upload/attachment/0087/6327/40d13e3f-3999-3451-a076-45a769770346.jpg[/img]

[img]http://dl2.iteye.com/upload/attachment/0087/6329/e363ed1b-2b35-3402-ac07-3e55587ff6c7.jpg[/img]

项目目录截图:
[img]http://dl2.iteye.com/upload/attachment/0087/6331/f8094654-cb41-3784-8aee-e708cd4f5c9c.jpg[/img]

代码介绍

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>


官方站点:[url]http://www.swfupload.org/[/url]
DEMO地址:[url]http://demo.swfupload.org/[/url]


SWFUplaod学习笔记阅读顺序
一、了解SWFUpload
[url]http://hanxin0311.iteye.com/blog/1915611[/url]

二、详细介绍SWFUpload
[url]http://hanxin0311.iteye.com/blog/1915615[/url]

三、struts2+swfUpload简单例子
[url]http://hanxin0311.iteye.com/blog/1915626[/url]

四、struts2+swfUpload深度整合
[url]http://hanxin0311.iteye.com/blog/1915628[/url]

SWFUpload像服务器传递参数
[url]http://hanxin0311.iteye.com/blog/1913946[/url]

SWFUpload接受服务器Action返回的参数
[url]http://hanxin0311.iteye.com/blog/1915644[/url]

SWFUpload中文乱码问题
[url]http://hanxin0311.iteye.com/blog/1915648[/url]


附件提供代码下载

项目开发工具 myeclipse8.5 [url=http://qd.baidupcs.com/file/5ee57cce0b179acfa96b757a6af55690?xcode=f23a0864bec0ad47e23a5ce12418f7c7074d0ecb9bea8f14&fid=3691663317-250528-1964120469&time=1375765520&sign=FDTAXER-DCb740ccc5511e5e8fedcff06b081203-mj%2BnwZk5r2hKL3ltpyqbDpYoP70%3D&to=qb&fm=Q,Q,U&expires=8h&rt=pr&r=365799409]下载代码[/url] (如果附件不能下载点击此处下载)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值