這僅僅只是一個(gè)小小的相冊管理,主要實(shí)現(xiàn)的功能:能夠?qū)崿F(xiàn)對圖片的上傳,統(tǒng)一瀏覽,單個(gè)下載,單個(gè)刪除,只能刪除自己上傳的文件。
現(xiàn)在對每個(gè)功能進(jìn)行單個(gè)的解釋:
圖片的上傳
圖片的上傳在之前的文章中寫的很清楚了,點(diǎn)擊打開鏈接:《JavaEE實(shí)現(xiàn)前后臺(tái)交互的文件上傳與下載》 。
在這個(gè)相冊管理中,就不是單一的文件傳了,還需要涉及到很多參數(shù)供其他功能模塊的使用
1
2
3
4
5
6
7
8
9
10
11
|
<span style= "font-size:24px;" > //上傳文件一般采用外面的 apache的上傳工具包 /* * 我們需要將上傳的文件放到指定的文件夾下 * 要獲得文件的信息 文件名 要存儲(chǔ)的文件夾(打散) uuid--dir * 解決中文問題存儲(chǔ)的文件名 uuid.jpg * 每個(gè)人都有自己的權(quán)限 ip * 上傳的時(shí)間 dt * 文件原先的真是名字 relName * 相片的說明 desc * 文件的擴(kuò)展名 ext *上面上傳一個(gè)圖片需要這么多的信息,,所以 采用 值對象《VO》封裝采用打亂文件夾存儲(chǔ),讓性能更優(yōu)。</span> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<span style= "font-size:24px;" > */ request.setCharacterEncoding( "utf-8" ); response.setContentType( "text/html;charset=utf-8" ); PrintWriter out=response.getWriter(); //讀文件用到apache的兩個(gè)包 //臨時(shí)存儲(chǔ)目錄 File f = new File( "f:/ex/temp" ); //存放臨時(shí)文件的目錄 DiskFileItemFactory dff= new DiskFileItemFactory( 1024 * 1024 * 20 , f); //允許臨時(shí)存儲(chǔ)文件大小為20M //解析的文件的工具 ServletFileUpload sf = new ServletFileUpload(dff); sf.setSizeMax( 1024 * 1024 * 50 ); //允許存儲(chǔ)容量為50M sf.setFileSizeMax( 1024 * 1024 * 20 ); //單個(gè)文件最大容量為 20M String path=getServletContext().getRealPath( "/upFile" ); //獲得文件的所在磁盤的路徑--》存儲(chǔ)位置 Photo p = new Photo(); InputStream in= null ; //拷貝流需要 boolean boo= false ; FileItem f0= null ; //用來刪除臨時(shí)文件 try { List<FileItem> list=sf.parseRequest(request); for (FileItem ff:list){</span> |
1
|
<span style= "font-size:24px;" > //前面的都是和之前的那個(gè)說的差不多,具體的統(tǒng)計(jì)參數(shù)就是從這里開始。</span> |
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
32
33
34
35
36
|
<span style= "font-size:24px;" > f0=ff; if (ff.isFormField()){ //這個(gè)為描述的內(nèi)容 String name=ff.getString( "utf-8" ); //采用utf-8的編碼方式去讀 p.setDesc(name); //1 文件的描述 } else { String name=ff.getName(); //獲得文件本框里面的內(nèi)容--->整個(gè)圖片的目錄 //System.out.println("name:"+name); String id=UtilsFactory.getUUid(); p.setId(id); //6 String dirs=UtilsFactory.getDir(id); //獲得文件夾目錄----使用uuid一一打散了的 p.setDir(dirs); //2 打亂之后的目錄 p.setDt(UtilsFactory.getDate()); //3 時(shí)間 String relname=name.substring(name.lastIndexOf( "/" )+ 1 ); p.setRelName(relname); //4 文件的真實(shí)名字 String ext=name.substring(name.lastIndexOf( "." )); p.setExt(ext); //5 文件擴(kuò)展名 p.setIp(request.getRemoteAddr()); //7 IP boo =MyDao.Add(p); //保存到xml文件中 if (boo){ //保存成功 path=path+ "/" +p.getDir(); File f1 = new File(path); //判斷文件的存儲(chǔ)路徑是否存在,不存在就創(chuàng)建 if (!f1.exists()){ f1.mkdirs(); } in=ff.getInputStream(); FileUtils.copyInputStreamToFile(in, new File(path+ "/" +p.getId()+p.getExt()) ); } } } } catch (FileUploadException e) { boo= false ; } finally { if (f0!= null ){ f0.delete(); //刪除臨時(shí)文件 } }</span> |
上傳除了統(tǒng)計(jì)參數(shù),我們需要將數(shù)據(jù)存儲(chǔ)的xml文件中,還需要將圖片存儲(chǔ)起來。等瀏覽的時(shí)候統(tǒng)一查看。
效果圖:
統(tǒng)一瀏覽
瀏覽基本就是全部將xml文件里面的數(shù)據(jù),讀出來,然后統(tǒng)一讀出來顯示。封裝在一個(gè)list中,將所有的photo數(shù)據(jù)封裝在list集合中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//查詢所有的對象然后封裝成一個(gè)list對象返回給前端 public static List<Photo> getAll(){ List<Photo> list= new ArrayList<Photo>(); Document dom =DocumentFactory.getDocument(); Element root=dom.getRootElement(); Iterator it=root.elementIterator(); //這是根節(jié)點(diǎn)遍歷器 while (it.hasNext()){ Element e=(Element) it.next(); //找到節(jié)點(diǎn) Photo p = new Photo(); //每一的photo地址不一樣,所以必須每次新開空間 p.setDesc(e.attributeValue( "desc" )); //文件描述符 p.setDir(e.attributeValue( "dir" )); //文件目錄 p.setDt(e.attributeValue( "dt" )); //時(shí)間 p.setExt(e.attributeValue( "ext" )); //文件擴(kuò)展名 p.setId(e.attributeValue( "id" )); //uuid生成的id p.setIp(e.attributeValue( "ip" )); p.setRelName(e.attributeValue( "relname" )); list.add(p); } return list; } |
具體代碼:
- //瀏覽相冊需要把所有的文件讀出來。需要一一去讀,所以需要去讀所有的xml文件
- response.setContentType("text/html;charset=utf-8");
- request.setCharacterEncoding("utf-8");
- PrintWriter out = response.getWriter();
- out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
- out.println("<HTML>");
- out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
- out.println(" <BODY>");
- List<Photo> list=MyDao.getAll();//獲得所有xml文件里面的內(nèi)容,數(shù)據(jù)全部封裝到list中
- String path=getServletContext().getContextPath();//進(jìn)入web之后要采用相對路徑才能訪問的到
- String ss=null;
- String imgs=null;
- String dt=null;
- String relName;
- String tt=null;
- String str = "<table border=1px width='80%'><tr><th>相冊名</th><th>時(shí)間</th><th>圖片顯示</th><th>操作</th>";
- out.write(str);
- for(Photo p:list){
- relName=p.getRelName();
- dt=p.getDt();
- imgs=path+"/upFile/"+p.getDir()+"/"+p.getId()+p.getExt();//完成的文件路徑加文件名
- ss="<tr><td>"+relName+"</td><td>"+dt+"</td><td>"+"<a href='"+imgs+"'><img style='border:0px' width='100' height='100' src='"+imgs+"' alt='圖片'/></a></td>";
- tt="<td><a href='DownFile?id="+p.getId()+"'>下載</a> <a href='MyDelelte?id="+p.getId()+"'>刪除圖片</a>"+"</td></tr>";//通過id來區(qū)分他們直接的區(qū)別 可以進(jìn)行刪除和下載
- out.write(ss);
- out.write(tt);
- }
效果:
單個(gè)下載
下載在之前的上傳和下載中都說的很清楚了,點(diǎn)擊打開鏈接: 《JavaEE實(shí)現(xiàn)前后臺(tái)交互的文件上傳與下載》。
下載的代碼中需要注意:需要設(shè)置相應(yīng)頭和文件名的傳輸
下面是具體的代碼:
- response.setContentType("application/force-download");//設(shè)置相應(yīng)頭,告訴瀏覽器這是下載文件
- request.setCharacterEncoding("utf-8");
- String id=request.getParameter("id");
- Photo p=MyDao.getSingalByid(id);//通過id獲得要下載的對象
- //寫入真實(shí)名字
- if(p!=null){
- String relName1=p.getRelName();
- String relName=URLEncoder.encode(relName1, "utf-8");
- response.setHeader("Content-Disposition","attachment;filename='"+relName+"'");//下載是那邊顯示的是原來文件名
- OutputStream out =response.getOutputStream();//寫文件時(shí)候需要
- //路徑
- String path="/upFile/"+p.getDir()+"/"+p.getId()+p.getExt();
- String path1 =getServletContext().getRealPath(path);
- System.out.println(path1);//檢測
- InputStream in=new FileInputStream(path1);
- byte[] b=new byte[1024];
- int len =0;
- while((len=in.read(b))!=-1){
- out.write(b, 0, len);
- }
- }else{
- response.setContentType("utf-8");
- PrintWriter pw =response.getWriter();
- pw.write("文件不存在無法下載");
- }
- }
效果圖:
刪除文件
刪除文件需要用到的技術(shù)相對其他功能
要匹配IP,ID這樣才能讓刪除的時(shí)候用權(quán)限
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
|
//刪除照片 public static Map<String , Object> deleteByid(String ip,String id) { Map<String, Object> map = new HashMap<String, Object>(); Document dom =DocumentFactory.getDocument(); Element ele=(Element) dom.selectSingleNode( "//photo[@id='" +id.trim()+ "']" );//xpath的使用 if (ele== null ){ map.put( "success" , false ); map.put( "msg" , "已經(jīng)刪除" ); return map; } else { String tempip=ele.attributeValue( "ip" ); if (!tempip.equals(ip)){ map.put( "success" , false ); map.put( "msg" , "你不能刪除別人的照片" ); return map; } else { map.put( "success" , true ); //訪問成功后,把數(shù)據(jù)分裝成一個(gè)值對象,返回給邏輯層 我們這樣直接刪除,只是xml文件里面的節(jié)點(diǎn)刪除,但是已經(jīng)存儲(chǔ)的文件是沒有刪除的 Photo p = new Photo(); p.setId(id); p.setExt(ele.attributeValue( "ext" )); p.setDir(ele.attributeValue( "dir" )); map.put( "photo" , p); //真正的數(shù)據(jù)刪除 ele.getParent().remove(ele); DocumentFactory.Save(); return map; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
request.setCharacterEncoding( "utf-8" ); response.setContentType( "text/html;charset=utf-8" ); PrintWriter out=response.getWriter(); String id =request.getParameter( "id" ); //從客戶端傳過來的消息 String ip =request.getRemoteAddr(); Map<String, Object> map =MyDao.deleteByid(ip,id); if (map.get( "success" ).equals( false )){ //這都是刪除不成功的 out.print( "消息為:" +map.get( "success" )); } else { Photo p =(Photo) map.get( "photo" ); //根據(jù)photo里面的文件名和路徑刪除文件 String path=getServletContext().getRealPath( "upFile" ); String filename=path+ "/" +p.getDir()+ "/" +p.getId()+p.getExt(); //文件的路徑包括文件名 System.out.println(filename); File f= new File(filename); if (f.exists()){ System.out.println(f.getName()); f.delete(); //刪除文件 } } response.sendRedirect( "LookPhoto" ); //重定向到顯示頁面 } |
整個(gè)項(xiàng)目的下載鏈接: 《javaWEB實(shí)現(xiàn)相冊管理的簡單功能》
正在處于學(xué)習(xí)中,剛學(xué)到這個(gè)知識(shí)點(diǎn),技術(shù)可能很落后了,但也愿意分享給大家,大家共同進(jìn)步。