騷包路技術(shù)菜
嗯哼
posts - 89, comments - 241, trackbacks - 0, articles - 1
::
首頁(yè)
:: ::
聯(lián)系
::
聚合
::
管理
Struts2文件上傳以及空指針異常解決
Posted on 2009-09-27 08:54
saobaolu
閱讀(4345)
評(píng)論(2)
編輯
收藏
所屬分類:
java基礎(chǔ)與算法
uploadFile.java
1
package
action;
2
3
import
java.io.File;
4
import
java.text.DateFormat;
5
import
java.text.SimpleDateFormat;
6
import
java.util.Date;
7
import
java.util.Random;
8
9
import
javax.servlet.ServletContext;
10
11
import
org.apache.commons.io.FileUtils;
12
import
org.apache.struts2.util.ServletContextAware;
13
14
import
com.opensymphony.xwork2.ActionSupport;
15
16
public
class
uploadFile
extends
ActionSupport
implements
ServletContextAware
{
17
18
private
static
final
long
serialVersionUID
=
-
5016873153441103539L
;
19
20
private
File doc;
21
private
String fileName;
22
private
String contentType;
23
24
private
ServletContext context;
25
26
public
void
setDoc(File file)
{
27
this
.doc
=
file;
28
}
29
30
public
void
setDocFileName(String fileName)
{
31
this
.fileName
=
fileName;
32
}
33
34
public
void
setDocContentType(String contentType)
{
35
this
.contentType
=
contentType;
36
}
37
38
public
void
setServletContext(ServletContext context)
{
39
this
.context
=
context;
40
}
41
42
public
String execute()
throws
Exception
{
43
String targetDirectory
=
context.getRealPath(
"
/upload
"
);
44
String targetFileName
=
generateFileName(fileName);
45
File target
=
new
File(targetDirectory, targetFileName);
46
47
FileUtils.copyFile(doc, target);
48
49
return
SUCCESS;
50
}
51
52
private
String generateFileName(String fileName)
{
53
DateFormat format
=
new
SimpleDateFormat(
"
yyMMddHHmmss
"
);
54
String formatDate
=
format.format(
new
Date());
55
56
int
random
=
new
Random().nextInt(
10000
);
57
58
int
position
=
fileName.lastIndexOf(
"
.
"
);
59
String extension
=
fileName.substring(position);
60
61
return
formatDate
+
random
+
extension;
62
}
63
}
64
struts.xml
1
<?
xml version="1.0" encoding="UTF-8"
?>
2
<!
DOCTYPE struts PUBLIC
3
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
5
<
struts
>
6
<
package
name
="uploadFile"
extends
="struts-default"
>
7
<
action
name
="uploadFile"
class
="action.uploadFile"
>
8
<
result
>
/jsp/up.jsp
</
result
>
9
</
action
>
10
</
package
>
11
</
struts
>
web.xml(Struts2的xml,非上傳的xml)
1
<?
xml version="1.0" encoding="UTF-8"
?>
2
<
web-app
version
="2.4"
3
xmlns
="http://java.sun.com/xml/ns/j2ee"
4
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
5
xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee
6
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
7
<
filter
>
8
<
filter-name
>
struts2
</
filter-name
>
9
<
filter-class
>
org.apache.struts2.dispatcher.FilterDispatcher
</
filter-class
>
10
</
filter
>
11
<
filter-mapping
>
12
<
filter-name
>
struts2
</
filter-name
>
13
<
url-pattern
>
/*
</
url-pattern
>
14
</
filter-mapping
>
15
</
web-app
>
up.jsp(上傳成功頁(yè)面)
1
<%
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
UTF-8
"
%>
2
<%
@ taglib prefix
=
"
s
"
uri
=
"
/struts-tags
"
%>
3
<%
4
String
path
=
request.getContextPath();
5
String
basePath
=
request.getScheme()
+
"
://
"
+
request.getServerName()
+
"
:
"
+
request.getServerPort()
+
path
+
"
/
"
;
6
%>
7
8
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
9
<
html
>
10
<
head
>
11
<
base
href
="<%=basePath%>"
>
12
13
<
title
>
My JSP 'up.jsp' starting page
</
title
>
14
15
<
meta
http-equiv
="pragma"
content
="no-cache"
>
16
<
meta
http-equiv
="cache-control"
content
="no-cache"
>
17
<
meta
http-equiv
="expires"
content
="0"
>
18
<
meta
http-equiv
="keywords"
content
="keyword1,keyword2,keyword3"
>
19
<
meta
http-equiv
="description"
content
="This is my page"
>
20
<!--
21
<link rel="stylesheet" type="text/css" href="styles.css">
22
-->
23
24
</
head
>
25
26
<
body
>
27
<
s:property
value
="contentType"
/><
br
/>
28
<
s:property
value
="dir"
/><
br
/>
29
</
body
>
30
</
html
>
31
上傳頁(yè)面:
1
<%
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
UTF-8
"
%>
2
<%
@ taglib prefix
=
"
s
"
uri
=
"
/struts-tags
"
%>
3
<%
4
String
path
=
request.getContextPath();
5
String
basePath
=
request.getScheme()
+
"
://
"
+
request.getServerName()
+
"
:
"
+
request.getServerPort()
+
path
+
"
/
"
;
6
%>
7
8
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
9
<
html
>
10
<
head
>
11
<
base
href
="<%=basePath%>"
>
12
13
<
title
>
最新產(chǎn)品發(fā)布頁(yè)
</
title
>
14
15
<
meta
http-equiv
="pragma"
content
="no-cache"
>
16
<
meta
http-equiv
="cache-control"
content
="no-cache"
>
17
<
meta
http-equiv
="expires"
content
="0"
>
18
<
meta
http-equiv
="keywords"
content
="keyword1,keyword2,keyword3"
>
19
<
meta
http-equiv
="description"
content
="This is my page"
>
20
<!--
21
<link rel="stylesheet" type="text/css" href="styles.css">
22
-->
23
24
</
head
>
25
26
<
body
>
27
<
s:form
action
="uploadFile"
method
="post"
enctype
="multipart/form-data"
>
28
<
s:file
name
= "doc"
label
="上傳視頻"
/>
29
<
s:submit
value
="上傳"
/>
30
</
s:form
>
31
</
body
>
32
</
html
>
33
很容易就會(huì)報(bào)錯(cuò),空指針異常。
原因在于上傳頁(yè)面<s:file name="
這里面的值與action的值不匹配
" />
可是我上傳成功之后,up.jsp頁(yè)面沒(méi)有傳入任何值,郁悶了~
新手,多指點(diǎn)指點(diǎn),謝謝~
沒(méi)有所謂的命運(yùn),只有不同的選擇!
Powered by:
BlogJava
Copyright © saobaolu
公告
留言簿
給我留言
查看公開(kāi)留言
查看私人留言
我參與的團(tuán)隊(duì)
南工技術(shù)圈(0/0)
隨筆分類
DataBase(5)
javaweb(7)
java基礎(chǔ)與算法(38)
Linux操作系統(tǒng)(10)
前端(12)
微信公共平臺(tái)開(kāi)發(fā)(1)
隨筆檔案
2013年8月 (1)
2010年7月 (1)
2010年6月 (6)
2010年3月 (1)
2010年1月 (5)
2009年12月 (3)
2009年11月 (5)
2009年10月 (4)
2009年9月 (12)
2009年8月 (1)
2009年7月 (4)
2009年6月 (4)
2009年5月 (7)
2009年4月 (7)
2009年3月 (16)
2009年2月 (2)
2008年10月 (5)
2008年9月 (2)
2008年8月 (3)
java
Java入門(mén)
java初學(xué)者視頻
專輯:J2EE入門(mén)教程-新浪播客
SQL語(yǔ)句教程
W3Cschool
墨白
My other site!
穿越大學(xué)的苦行僧
搜索
積分與排名
積分 - 91666
排名 - 631
最新評(píng)論
1.?re: 南陽(yáng)理工學(xué)院軟件學(xué)院Java開(kāi)發(fā)團(tuán)隊(duì)
@賀 怎么啦,保存不到數(shù)據(jù)庫(kù)數(shù)據(jù)?自己Debug 跟一下,問(wèn)題應(yīng)該不大。
--張志杰
2.?re: 南陽(yáng)理工學(xué)院軟件學(xué)院Java開(kāi)發(fā)團(tuán)隊(duì)
@賀
幫忙看下程序哪出錯(cuò)了
--賀
3.?re: 開(kāi)發(fā)一個(gè)學(xué)生管理系統(tǒng),將信息導(dǎo)入數(shù)據(jù)庫(kù)
評(píng)論內(nèi)容較長(zhǎng),點(diǎn)擊標(biāo)題查看
--賀
4.?re: MySQL5.0驅(qū)動(dòng)下載
的
--收到
5.?re: MySQL亂碼實(shí)戰(zhàn)解決ERROR 1366 (HY000): Incorrect string value: '
浮云,,都是浮云,,,,到my.ini改下mysqld編碼就好了
--張三
主站蜘蛛池模板:
亚洲国产日韩一区高清在线
|
国产精品观看在线亚洲人成网
|
亚洲精品无码国产
|
亚洲色偷偷色噜噜狠狠99
|
91在线视频免费观看
|
在线免费一区二区
|
久久综合九九亚洲一区
|
精品国产亚洲AV麻豆
|
91嫩草免费国产永久入口
|
亚洲男人在线无码视频
|
2020年亚洲天天爽天天噜
|
中国极品美軳免费观看
|
免费高清在线影片一区
|
亚洲精品永久www忘忧草
|
j8又粗又长又硬又爽免费视频
|
无码少妇一区二区浪潮免费
|
妞干网手机免费视频
|
中中文字幕亚洲无线码
|
国产真人无遮挡作爱免费视频
|
亚洲精品不卡视频
|
操美女视频免费网站
|
亚洲精品中文字幕无乱码
|
成人免费无码视频在线网站
|
亚洲精品资源在线
|
好吊妞在线成人免费
|
一级做a爱片特黄在线观看免费看
|
在线播放高清国语自产拍免费
|
中文字幕不卡高清免费
|
97久久精品亚洲中文字幕无码
|
91福利免费网站在线观看
|
色噜噜综合亚洲av中文无码
|
两个人看的www高清免费视频
|
久久精品亚洲精品国产色婷
|
一本色道久久88亚洲综合
|
亚洲欧美国产日韩av野草社区
|
日本亚洲免费无线码
|
亚洲日本国产乱码va在线观看
|
91精品啪在线观看国产线免费
|
亚洲天堂在线播放
|
免费看AV毛片一区二区三区
|
jizz中国免费
|