Struts2+ajaxFileUpload+JSP 页面无刷新上传
来源:互联网 发布:sql用户名和密码 编辑:程序博客网 时间:2024/05/18 13:10
Struts2+ajaxFileUpload+JSP 页面无刷新上传:
所依赖的jar及js有:下载struts2-json-plugin-2.1.8.1.jar;下载ajaxfileupload.js;下载jquery-1.2.6.min.js
直接分享代码:
web.xml(struts2的过滤器部分):
1
<
filter
>
2
<
filter-name
>struts2</
filter-name
>
3
<
filter-class
>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</
filter-class
>
4
</
filter
>
5
6
<
filter-mapping
>
7
<
filter-name
>struts2</
filter-name
>
8
<
url-pattern
>/*</
url-pattern
>
9
</
filter-mapping
>
upload.jsp:
01
<
tr
>
02
<
td
width
=
"30%"
>
03
选择文件:
04
</
td
>
05
<
td
width
=
"70%"
>
06
<
input
type
=
"file"
name
=
"upload"
id
=
"file_upload"
style
=
"width: 360px"
>
07
<
span
id
=
"span_upload"
></
span
>
08
<
img
alt
=
""
src="<%=path %>/img/loading.gif" id="loading" style="display: none; width: 20px; height: 20px">
09
</
td
>
10
</
tr
>
11
<
tr
>
12
<
td
colspan
=
"2"
align
=
"right"
>
13
<
input
value
=
"上传并解析"
type
=
"button"
id
=
"btn_upload"
align
=
"middle"
style
=
"width: 100px; height: 30px"
>
14
</
td
>
15
</
tr
>
01
<!-- 注意:包继承 “json-default” -->
02
<
package
name
=
"struts2"
extends
=
"json-default"
namespace
=
"/"
>
03
<!-- 上传文件 -->
04
<
action
name
=
"upload"
class
=
"fes.andy.frame.action.UploadAction"
method
=
"upload"
>
05
<
param
name
=
"savePath"
>/upload</
param
>
06
<
result
type
=
"json"
name
=
"success"
>
07
<
param
name
=
"contentType"
>text/html</
param
>
08
</
result
>
09
<
result
type
=
"json"
name
=
"error"
>
10
<
param
name
=
"contentType"
>html/html</
param
>
11
</
result
>
12
</
action
>
13
</
package
>
01
/**
02
* 上传并解析按钮
03
*/
04
$(
"#btn_upload"
).click(
function
(){
05
$(
"#loading"
).ajaxStart(
function
(){
06
$(
this
).show();
// 开始上传
07
}).ajaxComplete(
function
(){
08
$(
this
).hide();
// 上传结束
09
});
10
11
$.ajaxFileUpload({
12
url:
"upload"
,
// 文件上传服务器请求Action
13
secureuri:
true
,
// 安全提交,默认为false
14
fileElementId:
"file_upload"
,
// 文件类型的id
15
dataType:
"json"
,
// 返回值类型
16
success:
function
(data){
// 服务器响应成功
17
$(
"#span_title2"
).text(data.message).css(
"color"
,
"red"
);
18
$(
"#upload_result"
).show();
19
$(
this
).attr(
"src"
,
"/FESWork/img/right2.png"
);
20
},
21
error:
function
(data){
// 服务器响应失败
22
$(
"#span_title2"
).text(data.message).css(
"color"
,
"red"
);
23
}
24
})
25
return
false
;
26
});
UploadAction.java:
001
/**
002
* @包名 :fes.andy.frame.action<br>
003
* @文件名 :UploadAction.java<br>
004
* @类描述 :<br>
005
* @作者 :Andy.wang<br>
006
* @创建时间 :2013-9-11上午10:24:43<br>
007
* @更改人 :<br>
008
* @更改时间 :<br>
009
*/
010
package
fes.andy.frame.action;
011
012
import
java.io.File;
013
014
import
fes.andy.frame.util.FileUtil;
015
016
/**
017
* @包名 :fes.andy.frame.action<br>
018
* @文件名 :UploadAction.java<br>
019
* @类描述 :上传Jsp文件的Action<br>
020
* @作者 :Andy.wang<br>
021
* @创建时间 :2013-9-11上午10:24:43<br>
022
* @更改人 :<br>
023
* @更改时间 :<br>
024
*/
025
public
class
UploadAction
extends
SelfActionSupport {
026
027
private
static
final
long
serialVersionUID = 1L;
028
private
File upload;
// 上传文件
029
private
String uploadContentType;
// 上传文件类型的属性
030
private
String uploadFileName;
// 上传文件名
031
private
String savePath;
// 上传路径(struts.xml 中配置的参数)
032
033
private
String message =
""
;
034
035
/**
036
*
037
* @方法名 :upload<br>
038
* @方法描述 :上传文件<br>
039
* @创建者 :Andy.wang<br>
040
* @创建时间 :2013-9-11上午11:21:51 <br>
041
* @return 返回类型 :String
042
*/
043
public
String upload() {
044
try
{
045
File targetFile =
new
File(getServerPath() + savePath);
046
if
(!targetFile.exists()) {
047
targetFile.mkdirs();
048
}
049
targetFile =
new
File(targetFile, getUploadFileName());
050
boolean
b = FileUtil.copyPaste(getUpload(), targetFile);
051
if
(b)
052
message =
"文件上传成功,文件名为:"
+ uploadFileName +
"。"
;
053
else
054
message =
"文件上传失败,文件名为:"
+ uploadFileName +
"。"
;
055
}
catch
(Exception e) {
056
// TODO: handle exception
057
e.printStackTrace();
058
}
059
return
SUCCESS;
060
}
061
062
public
File getUpload() {
063
return
upload;
064
}
065
066
public
void
setUpload(File upload) {
067
this
.upload = upload;
068
}
069
070
public
String getUploadContentType() {
071
return
uploadContentType;
072
}
073
074
public
void
setUploadContentType(String uploadContentType) {
075
this
.uploadContentType = uploadContentType;
076
}
077
078
public
String getUploadFileName() {
079
return
uploadFileName;
080
}
081
082
public
void
setUploadFileName(String uploadFileName) {
083
this
.uploadFileName = uploadFileName;
084
}
085
086
public
String getSavePath() {
087
return
savePath;
088
}
089
090
public
void
setSavePath(String savePath) {
091
this
.savePath = savePath;
092
}
093
094
public
String getMessage() {
095
return
message;
096
}
097
098
public
void
setMessage(String message) {
099
this
.message = message;
100
}
101
102
}
SelfActionSupport.java中的
01
/**
02
*
03
* @方法名 :getServerPath<br>
04
* @方法描述 :获取服务器路径<br>
05
* @创建者 :Andy.wang<br>
06
* @创建时间 :2013-9-11上午11:11:49 <br>
07
* @return 返回类型 :String
08
*/
09
public
String getServerPath() {
10
serverPath = getClass().getProtectionDomain().getCodeSource()
11
.getLocation().getPath();
12
serverPath = serverPath.substring(
1
, serverPath.indexOf(
"WEB-INF"
));
13
return
serverPath;
14
}
0 0
- Struts2+ajaxFileUpload+JSP 页面无刷新上传
- Struts2 +jquery+ajaxfileupload 实现无刷新上传文件
- Struts2 +jquery+ajaxfileupload 实现无刷新上传图片
- struts2+ajaxfileupload完成单图片无刷新上传
- Struts2 +jquery+ajaxfileupload 实现无刷新上传文件
- ajaxFileUpload无刷新文件上传
- SpringMVC结合ajaxfileupload文件无刷新上传
- SpringMVC结合ajaxfileupload文件无刷新上传
- JSP无刷新上传
- struts2+iframe无刷新上传
- struts2无刷新上传头像
- jsp 无刷新上传文件
- JSP实现无刷新上传
- 使用ajaxFileUpload.js插件实现ajax无刷新上传文件
- 使用ajaxFileUpload.js插件实现ajax无刷新上传文件
- 利用ajaxfileupload插件实现无刷新文件上传
- ajaxfileupload.js+springMVC实现无刷新文件上传
- SpringMVC结合ajaxfileupload.js实现文件无刷新上传
- 【深入Java虚拟机】之四:类加载机制
- Android Service完全解析,关于服务你所需知道的一切(下)
- 第五周上机实践项目——三角形类的构造函数(1)
- 希尔排序
- GitHub 相关步骤
- Struts2+ajaxFileUpload+JSP 页面无刷新上传
- 百度贴吧自动抢2楼
- 数据类型
- 插入语排序
- linux mysql 相关命令
- iPhone开发之NSRunLoop的进一步理解
- 冒泡排序
- RadioButton点击事件
- 实现页面滚动条