公司做了一个相册模块,其中有一个要用多文件上传,可是使用的是很老的单文件上传的模式,感觉使用不是很方便,于是在网上搜索了一下,搜到了swfupload这个上传组件,于是尝试着使用struts2整合swfupload写了写,现在把代码放上来。
一、起始页代码
<%@ 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, upload2;
window.onload = function() {
upload1 = new SWFUpload({
// Backend Settings
upload_url: "PictureAction.action",
post_params: {"picSESSID" : "songhao"},
file_post_name: "file",
// File Upload Settings
file_size_limit : "102400", // 100MB
file_types : "*.*",
file_types_description : "All Files",
file_upload_limit : "10",
file_queue_limit : "0",
// Event Handler Settings (all my handlers are in the Handler.js file)
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 Settings
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
});
upload2 = new SWFUpload({
// Backend Settings
upload_url: "PictureAction.action",
post_params: {"SESSID" : "file"},
// File Upload Settings
file_size_limit : "200", // 200 kb
file_types : "*.jpg;*.gif;*.png",
file_types_description : "Image Files",
file_upload_limit : "10",
file_queue_limit : "5",
// Event Handler Settings (all my handlers are in the Handler.js file)
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 Settings
button_image_url : "images/XPButtonUploadText_61x22.png",
button_placeholder_id : "spanButtonPlaceholder2",
button_width: 61,
button_height: 22,
// Flash Settings
flash_url : "js/swfupload.swf",
swfupload_element_id : "flashUI2", // Setting from graceful degradation plugin
degraded_element_id : "degradedUI2", // Setting from graceful degradation plugin
custom_settings : {
progressTarget : "fsUploadProgress2",
cancelButtonId : "btnCancel2"
},
// Debug Settings
debug: false
});
}
</script>
</head>
<body>
<div id="header">
<h1 id="logo"><a href="../">SWFUpload</a></h1>
<div id="version">v2.2.0</div>
</div>
<div id="content">
<h2>Multi-Instance Demo</h2>
<form action="pictureAction" method="post" name="thisform" enctype="multipart/form-data">
<p>This page demonstrates how multiple instances of SWFUpload can be loaded on the same page.
It also demonstrates the use of the graceful degradation plugin and the queue plugin.</p>
<table>
<tr valign="top">
<td>
<div>
<div class="fieldset flash" id="fsUploadProgress1">
<span class="legend">Large File Upload Site</span>
</div>
<div style="padding-left: 5px;">
<span id="spanButtonPlaceholder1"></span>
<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>
</td>
<td>
<div>
<div class="fieldset flash" id="fsUploadProgress2">
<span class="legend">Small File Upload Site</span>
</div>
<div style="padding-left: 5px;">
<span id="spanButtonPlaceholder2"></span>
<input id="btnCancel2" type="button" value="Cancel Uploads" onclick="cancelQueue(upload2);" disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />
<br />
</div>
</div>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
二、upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>
</head>
<body>
<s:form action="PictureAction.action" enctype="multipart/form-data">
<s:file name="file"></s:file>
<s:submit></s:submit>
</s:form>
</body>
</html>
三、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.model.Picture;
import com.opensymphony.xwork2.ActionSupport;
public class PictureAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private File file;
private String fileFileName;
private String fileContentType;
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;
}
/**
* 用于实现上传功能
*/
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
//实现上传
InputStream is = new FileInputStream(file);
String root = ServletActionContext.getRequest().getRealPath("/upload");
System.out.println(this.getFileContentType() );
File deskFile = new File(root,this.getFileFileName());
OutputStream os = new FileOutputStream(deskFile);
byte [] bytefer = new byte[400];
int length = 0 ;
while((length = is.read(bytefer) )>0)
{
os.write(bytefer,0,length);
}
os.close();
is.close();
return SUCCESS;
}
}
上传不上去的原因的是没有新建一个文件夹 upload
分享到:
相关推荐
使用struts2整合swfupload,实现包含进度条的文件上传模块 SWFUpload的主要特点 * 可以同时上传多个文件; * 类似AJAX的无刷新上传; * 可以显示上传进度; * 良好的浏览器兼容性; * 兼容其他JavaScript库 (例如:...
SWFUpload上传组件的一个完整的例子,后台由Struts2完成,有上传进度显示,可以多文件上传,如果你想使用SWFUpload组件但还不知道怎样使用,可以下载下来运行一下试试,看一下源码就知道了。
自己写的一个DEMO,实现多文件上传,里面的样式使用的swfupload中的多文件上传的样式,只是实现了struts2+swfuplaod上传,喜欢的可以看看,
java+struts+swfupload文件上传下载 下载后可以直接运行
一个swfupload+struts2实现多文件上传的案例。
swfupload与struts2整合、完整java实例、
本示例解决了strut2+swfupload+extjs4文件上传过程中,后台struts接收不到上传文件的问题。而这个问题如果用servlet做后台可能就不存在。开发者可以用本例源码移植到自己项目中使用 。
JAVA+Struts2+swfupload上传下载,下载即可运行,支持多文件上传,Java代码有详细的代码注释。
ext struts2 swfupload 跨域文件上传
用的最新版本的 swfuploadv2.2.0.1,现在已经出了2.5版本的,但是还是测试版本的,可能会不稳定,所以我做了swfuploadv2.2.0.1这个版本的,大文件上传,90MB的文件5秒内就传好了 很快,效果很好用过就知道了
资源绝对可以用,经本人测试。。 原理:一次可以全选N个文件,然后将文件放入队列中,依次单个文件上传。可以设定上传文件大小类型,只需要更改路径就可以了
NULL 博文链接:https://firezhfox.iteye.com/blog/1734613
swfupload 上传 多文件 java 支持多文件上传,只需要在配置文件配置正确即可,后台是,struts2
SwfUpload多文件上传 struts1实现控制层
swfupload 多文件上传 选择多个文件 无刷新上传 使用框架struts2
利用Struts2上传文件时有时会速度过慢,这时候如果没有进度条显示的话会使得界面很不友好,所以我找了很多资料并调试成功,该文件就是项目文件,解压后直接用myeclipse8.5打开运行即可!
NULL 博文链接:https://longhahe.iteye.com/blog/1756339
一个struts的swfupload,界面精美
swfupload的应用,解决文件上传的问题
Java版 swfupload 上传显示 文件大小,速度,剩余时间 进度条 ssh2 开源 文件上传 下载 只要把下载的文件集成到 ssh2 的项目里面去就可以了。