這僅僅只是一個小小的相冊管理,主要實現的功能:能夠實現對圖片的上傳,統一瀏覽,單個下載,單個刪除,只能刪除自己上傳的文件。
現在對每個功能進行單個的解釋:
圖片的上傳
圖片的上傳在之前的文章中寫的很清楚了,點擊打開鏈接:《JavaEE實現前后臺交互的文件上傳與下載》 。
在這個相冊管理中,就不是單一的文件傳了,還需要涉及到很多參數供其他功能模塊的使用
1
2
3
4
5
6
7
8
9
10
11
|
<span style= "font-size:24px;" > //上傳文件一般采用外面的 apache的上傳工具包 /* * 我們需要將上傳的文件放到指定的文件夾下 * 要獲得文件的信息 文件名 要存儲的文件夾(打散) uuid--dir * 解決中文問題存儲的文件名 uuid.jpg * 每個人都有自己的權限 ip * 上傳的時間 dt * 文件原先的真是名字 relName * 相片的說明 desc * 文件的擴展名 ext *上面上傳一個圖片需要這么多的信息,,所以 采用 值對象《VO》封裝采用打亂文件夾存儲,讓性能更優。</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的兩個包 //臨時存儲目錄 File f = new File( "f:/ex/temp" ); //存放臨時文件的目錄 DiskFileItemFactory dff= new DiskFileItemFactory( 1024 * 1024 * 20 , f); //允許臨時存儲文件大小為20M //解析的文件的工具 ServletFileUpload sf = new ServletFileUpload(dff); sf.setSizeMax( 1024 * 1024 * 50 ); //允許存儲容量為50M sf.setFileSizeMax( 1024 * 1024 * 20 ); //單個文件最大容量為 20M String path=getServletContext().getRealPath( "/upFile" ); //獲得文件的所在磁盤的路徑--》存儲位置 Photo p = new Photo(); InputStream in= null ; //拷貝流需要 boolean boo= false ; FileItem f0= null ; //用來刪除臨時文件 try { List<FileItem> list=sf.parseRequest(request); for (FileItem ff:list){</span> |
1
|
<span style= "font-size:24px;" > //前面的都是和之前的那個說的差不多,具體的統計參數就是從這里開始。</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()){ //這個為描述的內容 String name=ff.getString( "utf-8" ); //采用utf-8的編碼方式去讀 p.setDesc(name); //1 文件的描述 } else { String name=ff.getName(); //獲得文件本框里面的內容--->整個圖片的目錄 //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 時間 String relname=name.substring(name.lastIndexOf( "/" )+ 1 ); p.setRelName(relname); //4 文件的真實名字 String ext=name.substring(name.lastIndexOf( "." )); p.setExt(ext); //5 文件擴展名 p.setIp(request.getRemoteAddr()); //7 IP boo =MyDao.Add(p); //保存到xml文件中 if (boo){ //保存成功 path=path+ "/" +p.getDir(); File f1 = new File(path); //判斷文件的存儲路徑是否存在,不存在就創建 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(); //刪除臨時文件 } }</span> |
上傳除了統計參數,我們需要將數據存儲的xml文件中,還需要將圖片存儲起來。等瀏覽的時候統一查看。
效果圖:
統一瀏覽
瀏覽基本就是全部將xml文件里面的數據,讀出來,然后統一讀出來顯示。封裝在一個list中,將所有的photo數據封裝在list集合中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//查詢所有的對象然后封裝成一個list對象返回給前端 public static List<Photo> getAll(){ List<Photo> list= new ArrayList<Photo>(); Document dom =DocumentFactory.getDocument(); Element root=dom.getRootElement(); Iterator it=root.elementIterator(); //這是根節點遍歷器 while (it.hasNext()){ Element e=(Element) it.next(); //找到節點 Photo p = new Photo(); //每一的photo地址不一樣,所以必須每次新開空間 p.setDesc(e.attributeValue( "desc" )); //文件描述符 p.setDir(e.attributeValue( "dir" )); //文件目錄 p.setDt(e.attributeValue( "dt" )); //時間 p.setExt(e.attributeValue( "ext" )); //文件擴展名 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文件里面的內容,數據全部封裝到list中
- String path=getServletContext().getContextPath();//進入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>時間</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來區分他們直接的區別 可以進行刪除和下載
- out.write(ss);
- out.write(tt);
- }
效果:
單個下載
下載在之前的上傳和下載中都說的很清楚了,點擊打開鏈接: 《JavaEE實現前后臺交互的文件上傳與下載》。
下載的代碼中需要注意:需要設置相應頭和文件名的傳輸
下面是具體的代碼:
- response.setContentType("application/force-download");//設置相應頭,告訴瀏覽器這是下載文件
- request.setCharacterEncoding("utf-8");
- String id=request.getParameter("id");
- Photo p=MyDao.getSingalByid(id);//通過id獲得要下載的對象
- //寫入真實名字
- 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();//寫文件時候需要
- //路徑
- 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("文件不存在無法下載");
- }
- }
效果圖:
刪除文件
刪除文件需要用到的技術相對其他功能
要匹配IP,ID這樣才能讓刪除的時候用權限
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" , "已經刪除" ); 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 ); //訪問成功后,把數據分裝成一個值對象,返回給邏輯層 我們這樣直接刪除,只是xml文件里面的節點刪除,但是已經存儲的文件是沒有刪除的 Photo p = new Photo(); p.setId(id); p.setExt(ele.attributeValue( "ext" )); p.setDir(ele.attributeValue( "dir" )); map.put( "photo" , p); //真正的數據刪除 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" ); //根據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" ); //重定向到顯示頁面 } |
整個項目的下載鏈接: 《javaWEB實現相冊管理的簡單功能》
正在處于學習中,剛學到這個知識點,技術可能很落后了,但也愿意分享給大家,大家共同進步。