第四十七讲:tapestry 与 swfupload上传

tapestry upload上传虽然简单易用,但并不能显示上传进度。swfupload是flash结合js写的上传,上传进度一目了然,下面是tapestry整合swfupload上传的代码:
 

首先去swfupload官网http://www.swfupload.org/下载文件,解压之后可看到,上传例子很多就以Simple Upload Demo为例,使用tapestry制作flash上传。
 

第一步:把swfupload.js、swfupload.queue.js、fileprogress.js、handlers.js、swfupload.swf拷贝到工程静态目录webapp\assets\js\swfupload目录下,文件夹没有的话可以自己在webapp下创建它。
 

第二步:把TestImageNoText_65x29.png图片拷贝到webapp\assets\images目录下,把default.css拷贝到webapp\assets\style目录下面。

以上步骤完成之后就看下面代码:

SWFUpload.java

/**
 * 项目名称:TapestryStart
 * 开发模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
 * 网址: http://www.flywind.org
 * 版本:1.0
 * 编写:飞风
 * 时间:2012-02-29
 */
package com.tapestry.app.pages;
 
import org.apache.tapestry5.annotations.Import;
 
@Import(library={"context:assets/js/swfupload/swfupload.js", 
"context:assets/js/swfupload/swfupload.queue.js", 
"context:assets/js/swfupload/fileprogress.js", 
"context:assets/js/swfupload/handlers.js"}, 
stylesheet="context:assets/style/default.css")
public class SWFUpload {
 
}

SWFUpload.tml

<html t:type="layout" title="tapestryStart Index"  t:sidebarTitle="Framework Version" 
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
<script type="text/javascript">
var swfu;
 
window.onload = function() {
var settings = {
flash_url : "${context:assets/js/swfupload/swfupload.swf}",
upload_url: "SWFUploadHander/",
//post_params: {"PHPSESSID" : "<?php echo session_id(); ?>"},
file_size_limit : "100 MB",
file_types : "*.*",
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: "${context:assets/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,
 
// 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>
<div id="content">
<h2>Simple Demo</h2>
<form id="form1" action="index.php" method="post" enctype="multipart/form-data">
<p>This page demonstrates a simple usage of SWFUpload.  It uses the Queue Plugin to simplify uploading or cancelling all queued files.</p>
 
<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" οnclick="swfu.cancelQueue();" disabled="disabled" style="margin-left: 2px; font-size: 8pt; height: 29px;" />
</div>
 
</form>
</div>
</html>

SWFUploadHander.java

/**
 * 项目名称:TapestryStart
 * 开发模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
 * 网址: http://www.flywind.org
 * 版本:1.0
 * 编写:飞风
 * 时间:2012-02-29
 */
package com.tapestry.app.pages;
 
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.ApplicationGlobals;
import org.apache.tapestry5.services.RequestGlobals;
import org.apache.tapestry5.upload.services.MultipartDecoder;
import org.apache.tapestry5.upload.services.UploadedFile;
 
import com.tapestry.app.util.UiUtils;
 
public class SWFUploadHander {
 
@Inject
private ApplicationGlobals applicationGlobals;
 
@Inject
private RequestGlobals requestGlobals;
 
@SuppressWarnings("unused")
@Property
private UploadedFile file;
 
@Inject
private MultipartDecoder decoder;
 
void onActivate() throws Exception{
UploadedFile file = decoder.getFileUpload("Filedata");
 
// 文件保存目录路径
String savePath = applicationGlobals.getServletContext().getRealPath("/uploadImages/") + "\\";
 
// 文件保存目录UR
//String saveUrl = getRequest().getContextPath() + "/uploadImages/";
 
//最大文件大小
long maxSize = 1048576;
 
// 检查目录
File uploadDir = new File(savePath);
 
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}
 
// 检查目录写权限
if (!uploadDir.canWrite()) {
System.out.println("上传目录没有写权限。");
return;
}
 
//文件夹名字
String dirName = "swfData";
// 创建文件夹
savePath += dirName + "/";
//saveUrl += dirName + "/";
File saveDirFile = new File(savePath);
if (!saveDirFile.exists()) {
saveDirFile.mkdirs();
}
 
String fileName = file.getFileName();
 
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String newFileName = df.format(new Date()) + "_" + UiUtils.createFileName(fileName);
 
File copied = new File(savePath + newFileName);
 
long fileSize = file.getSize();
 
if(fileSize > maxSize){
System.out.println("上传文件大小超过限制。");
return;
}
 
file.write(copied);
 
}
 
protected final HttpServletRequest getRequest() {
return requestGlobals.getHTTPServletRequest();
}
 
}
 

SWFUploadHander.tml

<html t:type="layout" title="tapestryStart Index"  t:sidebarTitle="Framework Version" 
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
 
</html>

UiUtils.java

package com.tapestry.app.util;
 
import java.util.Date;
import java.util.Random;
 
public class UiUtils {
public static String splitString(String str, String split) {
int index = str.lastIndexOf(split);
return str.substring(index + 1);
}
 
public static String splitStringPath(String str) {
str = str.replace("/", "\\");
int index = str.lastIndexOf("\\");
return str.substring(index + 1);
}
 
public static String createFileName(String name) {
        Long randomNum = new Random().nextLong();
        String radomStr = randomNum.toString().substring(2, 5);
        String accessoryAutoName = new Date().getTime() + radomStr;
        String extendedName = splitString(name, ".");
        String fileName = accessoryAutoName;
        if (extendedName != null) {
            fileName = accessoryAutoName + "." + extendedName;
        }
        return fileName;
    }
}
 

转载于:https://my.oschina.net/shootercn/blog/55549

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值