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

第四十七讲:tapestry 与 swfupload上传

发布时间:2020-12-15 17:22:12 所属栏目:百科 来源:网络整理
导读:tapestry upload上传虽然简单易用,但并不能显示上传进度。swfupload是flash结合js写的上传,上传进度一目了然,下面是tapestry整合swfupload上传的代码: ? 首先去swfupload官网http://www.swfupload.org/下载文件,解压之后可看到,上传例子很多就以Simple

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拷贝到工程静态目录webappassetsjsswfupload目录下,文件夹没有的话可以自己在webapp下创建它。
?

第二步:把TestImageNoText_65x29.png图片拷贝到webappassetsimages目录下,把default.css拷贝到webappassetsstyle目录下面。

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

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" onclick="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;
? ? }
}

(编辑:李大同)

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

    推荐文章
      热点阅读