SWFUpload简单使用例子 Java版(JSP)
2012-10-19 18:32
621 查看
SWFUpload官方的例子都是PHP的,在这里提供一个Java版的最简单的使用例子,使用JSP页面完成所有操作。
实现上传,分为三步:
1、JavaScript设置SWFUpload部分(与官方例子类似):
Js代码
var upload;
window.onload = function() {
upload = new SWFUpload({
// 处理文件上传的url
upload_url: "${pageContext.request.contextPath}/swfupload/example.jsp?upload=1",
// 上传文件限制设置
file_size_limit : "10240", // 10MB
file_types : "*.jpg;*.gif;*.png", //此处也可以修改成你想限制的类型,比如:*.doc;*.wpd;*.pdf
file_types_description : "Image Files",
file_upload_limit : "0",
file_queue_limit : "1",
// 事件处理设置(所有的自定义处理方法都在handler.js文件里)
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 : "swfupload/xpbutton.png", // 按钮图标
button_placeholder_id : "spanButtonPlaceholder",
button_width: 61,
button_height: 22,
// swf设置
flash_url : "swfupload/swfupload.swf",
custom_settings : {
progressTarget : "fsUploadProgress",
cancelButtonId : "btnCancel"
},
// Debug 设置
debug: false
});
}
2、页面显示部分:
Html代码
<div class="flash" id="fsUploadProgress"></div>
<div style="padding-left: 5px;">
<span id="spanButtonPlaceholder"></span>
<input id="btnCancel" type="button" value="取消" onclick="cancelQueue(upload);"
disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />
</div>
3、Java处理文件上传部分:
Java代码
String uploadSign = request.getParameter("upload");
String rootPath = request.getParameter("rootPath");
String path = request.getParameter("path");
if(rootPath == null) rootPath = "";
rootPath = rootPath.trim();
if(rootPath.equals("")){
rootPath = application.getRealPath("/swfupload/files");
}
if(path == null) {
path = rootPath;
}else{
path = new String(Base64.decodeBase64(path.getBytes()));
}
//上传操作
if(null != uploadSign && !"".equals(uploadSign)){
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
//upload.setHeaderEncoding("UTF-8");
try{
List items = upload.parseRequest(request);
if(null != items){
Iterator itr = items.iterator();
while(itr.hasNext()){
FileItem item = (FileItem)itr.next();
if(item.isFormField()){
continue;
}else{
//以当前精确到秒的日期为上传的文件的文件名
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddkkmmss");
String type = item.getName().split("\\.")[1];//获取文件类型
File savedFile = new File(path,sdf.format(new Date())+"."+type);
item.write(savedFile);
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
实现上传,分为三步:
1、JavaScript设置SWFUpload部分(与官方例子类似):
Js代码
var upload;
window.onload = function() {
upload = new SWFUpload({
// 处理文件上传的url
upload_url: "${pageContext.request.contextPath}/swfupload/example.jsp?upload=1",
// 上传文件限制设置
file_size_limit : "10240", // 10MB
file_types : "*.jpg;*.gif;*.png", //此处也可以修改成你想限制的类型,比如:*.doc;*.wpd;*.pdf
file_types_description : "Image Files",
file_upload_limit : "0",
file_queue_limit : "1",
// 事件处理设置(所有的自定义处理方法都在handler.js文件里)
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 : "swfupload/xpbutton.png", // 按钮图标
button_placeholder_id : "spanButtonPlaceholder",
button_width: 61,
button_height: 22,
// swf设置
flash_url : "swfupload/swfupload.swf",
custom_settings : {
progressTarget : "fsUploadProgress",
cancelButtonId : "btnCancel"
},
// Debug 设置
debug: false
});
}
2、页面显示部分:
Html代码
<div class="flash" id="fsUploadProgress"></div>
<div style="padding-left: 5px;">
<span id="spanButtonPlaceholder"></span>
<input id="btnCancel" type="button" value="取消" onclick="cancelQueue(upload);"
disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />
</div>
3、Java处理文件上传部分:
Java代码
String uploadSign = request.getParameter("upload");
String rootPath = request.getParameter("rootPath");
String path = request.getParameter("path");
if(rootPath == null) rootPath = "";
rootPath = rootPath.trim();
if(rootPath.equals("")){
rootPath = application.getRealPath("/swfupload/files");
}
if(path == null) {
path = rootPath;
}else{
path = new String(Base64.decodeBase64(path.getBytes()));
}
//上传操作
if(null != uploadSign && !"".equals(uploadSign)){
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
//upload.setHeaderEncoding("UTF-8");
try{
List items = upload.parseRequest(request);
if(null != items){
Iterator itr = items.iterator();
while(itr.hasNext()){
FileItem item = (FileItem)itr.next();
if(item.isFormField()){
continue;
}else{
//以当前精确到秒的日期为上传的文件的文件名
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddkkmmss");
String type = item.getName().split("\\.")[1];//获取文件类型
File savedFile = new File(path,sdf.format(new Date())+"."+type);
item.write(savedFile);
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
相关文章推荐
- SWFUpload简单使用例子 Java版(JSP)
- SWFUpload简单使用例子 Java版(JSP)
- SWFUpload简单使用样例 Java版(JSP)
- 使用Java读取Word文件的简单例子分享
- 一个java生成与使用WebService的简单例子
- java 使用 comet4j 主动向客户端推送信息 简单例子
- Java 序列化--- Kryo使用简单例子
- 使用eclipse在jsp上显示水晶报表(一)最简单例子
- java 使用 comet4j 主动向客户端推送信息 简单例子
- java 使用 comet4j 主动向客户端推送信息 简单例子
- java使用UDP来进行客户端和服务器端通信的简单例子
- java爬取网页内容 简单例子(1)——使用正则表达式
- java 使用 comet4j 主动向客户端推送信息 简单例子
- java 使用 comet4j 主动向客户端推送信息 简单例子
- 使用JSP + JAVABEAN + XML 开发的一个例子
- Jsp+servlet+javabean使用Lucene3.6.2做一个简单数据库搜索的总结(2)
- 使用Java进行UG二次开发:简单的例子(上)
- 转:jsp 调用java简单例子
- 举一个简单例子来说明:如何在JSP中使用JavaBean
- 一个java生成与使用WebService的简单例子