瀑布流ajax分页
时间:2014-04-29 20:28:33
收藏:0
阅读:707
index.jsp
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 6 <title>图片</title> 7 <script src="${pageContext.request.contextPath}/dwz/js/jquery-1.7.2.js" type="text/javascript"></script> 8 <script src="${pageContext.request.contextPath}/common/waterfall/waterfall.js" type="text/javascript"></script> 9 <link rel="stylesheet" href="${pageContext.request.contextPath}/common/waterfall/waterfall.css" type="text/css" media="screen" /> 10 <script type="text/javascript" src="${pageContext.request.contextPath}/common/lightview/js/google/swfobject.js"></script> 11 <!--[if lt IE 9]> 12 <script type="text/javascript" src="${pageContext.request.contextPath}/common/lightview/js/excanvas/excanvas.js"></script> 13 <![endif]--> 14 <script type="text/javascript" src="${pageContext.request.contextPath}/common/lightview/js/spinners/spinners.min.js"></script> 15 <script type="text/javascript" src="${pageContext.request.contextPath}/common/lightview/js/lightview/lightview.js"></script> 16 <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/common/lightview/css/lightview/lightview.css" /> 17 </head> 18 <body> 19 <div id="container"> 20 ${init} 21 </div> 22 <script> 23 var opt = { 24 getResource:function(index,render){//index为已加载次数,render为渲染接口函数,接受一个dom集合或jquery对象作为参数。通过ajax等异步方法得到的数据可以传入该接口进行渲染,如 render(elem) 25 if(index>= ${totalPageNum}){// index=index%${totalPageNum}+1; 26 return $(""); 27 }else{ 28 var html = $.ajax({ 29 type:"post", 30 url:"${pageContext.request.contextPath}/multimedia/image/index.do", 31 data:({index:index}), 32 async:false, 33 dataType:"html" 34 }).responseText; 35 return $(html); 36 } 37 }, 38 auto_imgHeight : true, 39 insert_type : 1 40 } 41 $("#container").waterfall(opt); 42 </script> 43 </body> 44 </html>
DisplayImageAction.java
1 package com.movo.multimedia.struts.action; 2 3 import java.awt.geom.AffineTransform; 4 import java.awt.image.AffineTransformOp; 5 import java.awt.image.BufferedImage; 6 import java.io.File; 7 import java.io.PrintWriter; 8 import java.util.ArrayList; 9 import java.util.HashSet; 10 import java.util.List; 11 import java.util.Set; 12 13 import javax.imageio.ImageIO; 14 import javax.servlet.http.HttpServletRequest; 15 import javax.servlet.http.HttpServletResponse; 16 17 import org.apache.commons.io.FileUtils; 18 import org.apache.struts.action.Action; 19 import org.apache.struts.action.ActionForm; 20 import org.apache.struts.action.ActionForward; 21 import org.apache.struts.action.ActionMapping; 22 23 import com.movo.util.Utils; 24 25 public class DisplayImageAction extends Action { 26 27 @Override 28 public ActionForward execute(ActionMapping mapping, ActionForm form, 29 HttpServletRequest request, HttpServletResponse response) 30 throws Exception { 31 PrintWriter out = response.getWriter(); 32 String[] imageSuffix = new String[]{"jpg","jpeg","bmp","png","gif"}; 33 String[] flashSuffix = new String[]{"swf"}; 34 String[] quicktimeSuffix = new String[]{}; 35 String basePath = "D:\\2011-12-28\\tomcat\\apache-tomcat-6.0.35\\webapps\\newwork\\"; 36 String dirPath = basePath + "userfiles\\imageLibrary\\"; 37 String dirThumbPath = basePath + "userfiles\\imageThumbLibrary\\"; 38 Set<File> set = new HashSet<File>(); 39 set = this.getFiles(dirPath,set); 40 int index = 0; 41 try{ 42 index = Integer.parseInt(request.getParameter("index")); 43 }catch(Exception e){ 44 } 45 if(index == -1){ 46 request.getRequestDispatcher("indexDetails.jsp").forward(request,response); 47 }else{ 48 int perCount = 30; 49 StringBuffer buf = new StringBuffer(); 50 List<File> list = new ArrayList<File>(); 51 list.addAll(set); 52 int totalPageNum = (list.size() + perCount - 1) / perCount; 53 for(int i = ((index) * perCount);i < (index + 1) * perCount && i < list.size();i++){ 54 String path = list.get(i).getAbsolutePath(); 55 String suffix = Utils.getInstance().getExtByUrl(list.get(i).getName()); 56 boolean imageTag = false; 57 boolean flashTag = false; 58 boolean quicktimeTag = false; 59 for(String str : imageSuffix){ 60 if(suffix.equalsIgnoreCase(str)){ 61 imageTag = true; 62 break; 63 } 64 } 65 if(!imageTag){ 66 if(!quicktimeTag){ 67 for(String str : quicktimeSuffix){ 68 if(suffix.equalsIgnoreCase(str)){ 69 quicktimeTag = true; 70 break; 71 } 72 } 73 }else{ 74 for(String str : flashSuffix){ 75 if(suffix.equalsIgnoreCase(str)){ 76 flashTag = true; 77 break; 78 } 79 } 80 } 81 } 82 String thumbPath = dirThumbPath; 83 if(imageTag){ 84 thumbPath += path.replace(dirPath,""); 85 }else if(flashTag){ 86 thumbPath += "15330T5C-11.png"; 87 }else if(quicktimeTag){ 88 thumbPath += "15330U928-22.png"; 89 } 90 File thumbFile = new File(thumbPath); 91 if(!thumbFile.exists()){ 92 File tempFile = new File(thumbFile.getAbsolutePath().replace(thumbFile.getName(),"")); 93 tempFile.mkdirs(); 94 if(imageTag){ 95 this.thumbnails(new File(path),thumbFile); 96 }else if(flashTag){ 97 String temp = request.getSession().getServletContext().getRealPath("\\") + "images\\15330T5C-11.png"; 98 FileUtils.copyFile(new File(temp),thumbFile); 99 }else if(quicktimeTag){ 100 String temp = request.getSession().getServletContext().getRealPath("\\") + "images\\15330U928-22.png"; 101 FileUtils.copyFile(new File(temp),thumbFile); 102 } 103 } 104 path = path.replace(basePath,Utils.BASEURL); 105 path = path.replace("\\","/"); 106 thumbPath = thumbPath.replace(basePath,Utils.BASEURL); 107 thumbPath = thumbPath.replace("\\","/"); 108 if(imageTag){ 109 buf.append("<div class=\"cell\"><a class=\"lightview\" data-lightview-type=\"image\" data-lightview-group=\"test\" data-lightview-caption=\"").append(list.get(i).getAbsolutePath()).append("\" data-lightview-title=\"").append(list.get(i).getName()).append("\" href=\"").append(path).append("\"><img width=\"193\" src=\"").append(thumbPath).append("\" /></a></div>"); 110 }else if(flashTag){ 111 buf.append("<div class=\"cell\"><a class=\"lightview\" data-lightview-type=\"flash\" data-lightview-group=\"test\" data-lightview-title=\"").append(list.get(i).getName()).append("\" href=\"").append(path).append("\"><img width=\"193\" src=\"").append(thumbPath).append("\" /></a></div>"); 112 }else if(quicktimeTag){ 113 buf.append("<div class=\"cell\"><a class=\"lightview\" data-lightview-type=\"quicktime\" data-lightview-group=\"test\" data-lightview-options=\"params:{controller: true,autoplay: true}\" data-lightview-title=\"").append(list.get(i).getName()).append("\" href=\"").append(path).append("\"><img width=\"193\" src=\"").append(thumbPath).append("\" /></a></div>"); 114 } 115 } 116 if(index == 0){ 117 request.setAttribute("init",buf.toString()); 118 request.setAttribute("totalPageNum",totalPageNum); 119 request.getRequestDispatcher("index.jsp").forward(request,response); 120 }else{ 121 out.println(buf.toString()); 122 } 123 } 124 return null; 125 } 126 public Set<File> getFiles(String dirPath,Set<File> set){ 127 File dir = new File(dirPath); 128 if(dir.isDirectory() && dir.exists()){ 129 File[] files = dir.listFiles(); 130 if(files != null){ 131 for(File file : files){ 132 if(file.isDirectory()){ 133 this.getFiles(file.getAbsolutePath(),set); 134 }else{ 135 set.add(file); 136 } 137 } 138 } 139 }else if(dir.isFile() && dir.exists()){ 140 set.add(dir); 141 } 142 return set; 143 } 144 private void thumbnails(File fi,File fo){ 145 try { 146 AffineTransform transform = new AffineTransform(); 147 BufferedImage bis = ImageIO.read(fi); 148 149 int w = bis.getWidth(); 150 int h = bis.getHeight(); 151 152 int nw = 193; 153 int nh = (nw * h) / w; 154 155 double sx = (double)nw / w; 156 double sy = (double)nh / h; 157 158 transform.setToScale(sx,sy); 159 160 AffineTransformOp ato = new AffineTransformOp(transform, null); 161 BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR); 162 ato.filter(bis,bid); 163 ImageIO.write(bid, "jpg", fo); 164 } catch(Exception e) { 165 e.printStackTrace(); 166 } 167 } 168 }
效果如下图:
评论(0)