一、文件上傳
思想:圖片是以流的方式進行讀取,上傳的圖片存放在服務器端的某個文件夾中,數據庫中保存的是上傳的圖片的地址。
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)方法同上
Struts實現單文件上傳、修改、刪除(以圖片爲例)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.