xiaolutan
年輕沒有失敗成功是需要積累的
Struts實現單文件上傳、修改、刪除(以圖片為例)
一、文件上傳
思想:圖片是以流的方式進行讀取,上傳的圖片存放在服務器端的某個文件夾中,數據庫中保存的是上傳的圖片的地址。
1
.ActionForm(userForm)
private
FormFile image;
//
用于封裝從jsp頁面傳過來的文本類型的屬性
2
.pojo(User)
private
String path;
//
用于存放上傳圖片的地址,數據庫中需要添加存放該地址的字段
3
.jsp
<
html:form action
=
"
"
enctype
=
"
multipart/form-data
"
>
//
form設置了enctype屬性后就是二進制傳輸數據了
<
html:file property
=
"
image
"
></
html:file
>
</
html:form
>
4
.action
String basePath
=
this
.getServlet().getServletContext().getRealPath(
"
/
"
);
//
獲得服務器的基路徑
String path
=
"
/uploadImage/
"
+
userService.picturePath(userForm.getImage());
//
上傳圖片保存的相對路徑,圖片 名通過userService中的picturePath方法進行處理
String endString
=
userForm.getImage().getFileName().substring
(userForm.getImage().getFileName().lastIndexOf(
"
.
"
)
+
1
);
//
獲取圖片的后綴名
5
.service(userService)
if
(
"
jpg
"
.equals(endString)
||
"
png
"
.equals(endString)
||
"
gif
"
.equals(endString)
||
"
JPG
"
.equals(endString)
||
"
PNG
"
.equals(endString)
||
"
GIF
"
.equals(endString))
{
//
限制上傳圖片的類型
userDAO.saveImage(image,basePath,path);
//
保存圖片
user.setPath(path);
//
將上傳圖片的路徑(地址)進庫
}
else
{
user.setPath(
"
"
);
//
上傳的圖片不符合要求,在數據庫中設置為空
}
6
.dao(userDAO)
public
String picturePath(FormFile image)
{
//
處理上傳的圖片名,UUIDGenerator詳見相關日志
String filename
=
"
"
;
//
初始化變量
UUIDGenerator g
=
new
UUIDGenerator();
//
創建UUIDGenerator類的一個對象
filename
=
image.getFileName();
//
將圖片的文件名賦值給變量
if
(filename.length()
>
0
)
{
filename
=
filename.substring(filename.lastIndexOf(
"
.
"
));
//
將文件名除了后綴名的部分賦給filename變量
}
filename
=
(String)g.generate()
+
filename;
//
對上傳的圖片文件名進行處理,以免出現上傳的圖片名相同
return
filename;
}
public
void
saveImage(FormFile image,String basePath,String path)
throws
IOException
{
//
上傳圖片
FileOutputStream fos
=
null
;
//
文件輸出流
BufferedInputStream in
=
null
;
//
緩沖的輸入流
BufferedOutputStream out
=
null
;
//
緩沖的輸出流
try
{
fos
=
new
FileOutputStream(basePath
+
path);
//
創建一個向指定File對象表示的文件中寫入數據的輸出流
in
=
new
BufferedInputStream(image.getInputStream());
//
創建BufferedInputStream并保存其參數,即輸入流 in,以便將來使用
out
=
new
BufferedOutputStream(fos);
//
創建一個新的緩沖輸出流,以將數據寫入指定的基礎輸出流
byte
[] buf
=
new
byte
[
8192
];
//
創建一個字節緩沖數組,用于指定每次寫入的字節大小(8192=8k)
int
readSize;
while
((readSize
=
in.read(buf))
!=-
1
)
{
//
判斷緩沖數組中是否還有數據
out.write(buf,
0
, readSize);
//
將指定buf數組中從偏移量0開始的readSize個字節寫入此緩沖的輸出流
out.flush();
//
關閉緩沖的輸出流
}
}
catch
(FileNotFoundException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if
(out
!=
null
)
{
try
{
out.close();
}
catch
(RuntimeException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
}
if
(in
!=
null
)
{
try
{
in.close();
}
catch
(RuntimeException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
}
if
(fos
!=
null
)
{
try
{
fos.close();
}
catch
(RuntimeException e)
{
//
TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
二、文件刪除
思路:上傳的文件存放在某個文件夾中,該圖片的地址存放在數據庫中,因此,在刪除圖片的時候,不僅要刪除它在數據庫中的地址,而且還要刪除在文件夾中的文件,刪除圖片之前要獲取該圖片的絕對路徑。
1
.action(userAction)
String basepath
=
this
.getServlet().getServletContext().getRealPath(
"
/
"
);
//
獲得服務器基路徑
2
.service(userService)
userDAO.deleteImage(uId,basepath);
//
刪除圖片
userDAO.deleteUser(uId);
//
刪除圖片在數據庫中的路徑
3
.dao(userDAO)
public
void
deleteImage(Integer uId,String basepath)
{
User user
=
this
.getUserById(uId);
//
取出某個用戶對象(獲得圖片的相對路徑)
String path
=
user.getPath();
//
將圖片的相對路徑賦值
if
(path
!=
null
)
{
File file
=
new
File(basepath
+
path);
//
根據絕對路徑創建一個文件對象
file.delete();
//
刪除文件
}
}
三、修改文件
思路:先將原來的圖片刪除,然后重新上傳一張圖片
1
.jsp(edit.jsp)
<
html:hidden property
=
"
path
"
/>
//
獲得原來圖片的路徑放在隱藏域中(尤其要注意)
<
html:file property
=
"
image
"
/>
2
.action(userAction)
String basepath
=
this
.getServlet().getServletCotext().getRealPath(
"
/
"
);
//
獲得服務器基路徑
String path
=
null
;
if
(userForm.getImage().getFileSize()
>
0
)
{
//
判斷是否需要修改圖片
path
=
"
/uploadImage/
"
+
userForm.getImage();
}
userService.modify(basepath,path,userForm.getImage());
//
調用修改的方法
3
.service(userService)
if
(user.getPath()
!=
null
&&
path
!=
null
)
{
//
需要修改圖片的情況
userDAO.deleteImage(basepath
+
path);
//
根據絕對路徑刪除圖片
userDAO.saveImage(image,basepath,path);
//
根據相對路徑重新上傳一張圖片
user.setPath(path);
//
將新路徑保存到user對象中
}
userDAO.modify(id,user);
4
.dao(userDAO)方法同上
posted on 2012-01-07 16:27
巨石
閱讀(1270)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © 巨石
<
2012年1月
>
日
一
二
三
四
五
六
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
導航
BlogJava
首頁
新隨筆
聯系
聚合
管理
統計
隨筆 - 1
文章 - 0
評論 - 0
引用 - 0
常用鏈接
我的隨筆
我的評論
我的參與
留言簿
給我留言
查看公開留言
查看私人留言
隨筆檔案
2012年1月 (1)
搜索
最新評論
主站蜘蛛池模板:
阳高县
|
大渡口区
|
闻喜县
|
南城县
|
太湖县
|
共和县
|
全州县
|
古浪县
|
潜山县
|
普陀区
|
宁海县
|
大关县
|
镇远县
|
商丘市
|
扶绥县
|
岳西县
|
交城县
|
共和县
|
塔城市
|
阳春市
|
内黄县
|
武川县
|
巴林左旗
|
镇巴县
|
安化县
|
义乌市
|
林周县
|
云浮市
|
沧州市
|
渝北区
|
宜兰县
|
滨州市
|
林周县
|
呼伦贝尔市
|
奎屯市
|
三江
|
阿拉善盟
|
凤凰县
|
达孜县
|
镇沅
|
皋兰县
|