在运行ajax的图片浏览器的时候,仅有第一次运行的情况是正确的,能正确显示图片。
JS的代码为:
var store = new Ext.data.JsonStore({ url: '../ImagesServlet', root: 'images', fields: ['name', 'url',{name:'size', type: 'float'}, {name:'lastmod', type:'date', dateFormat:'timestamp'}] });
JS调用ImageServlet,取得json类型的数据。然后根据json的数据显示图片。
ImageServlet代码为:
/** * */ package org.eimhe; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author liheng * ImagesServlet.java * 2009-2-23 */ public class ImagesServlet extends HttpServlet{ private boolean is_first = true; private StringBuffer jsonString = new StringBuffer(); protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { processRequest(request,response); } protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ processRequest(request,response); } protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { //json "[images:{{name:'',url:'',size:'',lastmod:''},{}}]" ServletContext ctx = getServletContext(); String cs = ctx.getRealPath("/upload"); //String cs = "c:/upload"; File f = new File(cs); /** * if(jsonString != null) * { * System.out.println("the cache is: "+jsonString.toString()); * jsonString = new StringBuffer(); * is_first = true; * } */ jsonString.append("{\"images\":["); //append json string head if(f.isDirectory()) { File[] fs = f.listFiles(new picFilter()); for(int i=0,len=fs.length;i<len;i++){ if(fs[i].isFile()) { if(is_first) { is_first = false; } else { jsonString.append(","); } jsonString.append("{\"name\":\""+fs[i].getName()+"\",\"url\":\"../upload/"+fs[i].getName()+"\",\"size\":"+fs[i].length()+",\"lastmod\":"+fs[i].lastModified()+"}"); //jsonString.append("{\"name\":\""+fs[i].getName()+"\",\"url\":\""+fs[i].getPath()+"\",\"size\":"+fs[i].length()+",\"lastmod\":"+fs[i].lastModified()+"}"); } else if(fs[i].isDirectory()) { getJsonStringForDirectory(fs[i],fs[i].getName()); } } } jsonString.append("]}"); //append json string bottom response.setContentType("text/html"); response.setHeader("Pragma","No-cache"); response.setDateHeader("Expires",0); response.setHeader("Cache-Control","no-cache"); PrintWriter out = response.getWriter(); out.print(jsonString.toString()); out.close(); } protected void getJsonStringForDirectory(File f,String directoryName) { File[] ifile = f.listFiles(new picFilter()); for(int i=0,len=ifile.length;i<len;i++) { if(ifile[i].isFile()) { if(is_first) { is_first = false; } else { jsonString.append(","); } jsonString.append("{\"name\":\""+ifile[i].getName()+"\",\"url\":\"../upload/"+directoryName+"/"+ifile[i].getName()+"\",\"size\":"+ifile[i].length()+",\"lastmod\":"+ifile[i].lastModified()+"}"); } else if(ifile[i].isDirectory()) { getJsonStringForDirectory(ifile[i],directoryName+"/"+ifile[i].getName()); } } } }
刷新的时候,图片老是不能正常显示,于是怀疑是现实的json的格式有问题,发现没刷新一次,json的数据就多一些。原来是jsonString 和is_first都缓存了数据,在第二次运行的时候没有设置为默认值,导致json的格式错误。
去掉代码中注释的那块,图片能正常显示啦。