时间:2023-07-04 04:21:01 | 来源:网站运营
时间:2023-07-04 04:21:01 来源:网站运营
装X - 建立自己的斗图网站库:之前加过一个斗图群,看到很多经典的表情,然后就收藏到了QQ, 迫于本屌丝开不起某Q会员,就只能收藏到本地,无法在其他的电脑上使用。久而久之收藏的也越来越多了,但是管理就成了问题,每次合适的场景想起一张合适的图,但怎么都找不到了,于是乎,本屌丝打算自己写一个斗图收藏网站。// 此处为doPost的全部内容public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); ReadList rl = new ReadList(); String json = rl.read(); // 此处返回一个json格式的字符串,前端在进行 JSON.parse(); out.println(json); out.flush(); out.close();}
package com.cxw.face;import java.io.File;public class ReadList { public String read() { // windwos环境下开发的测试路径(本地开发时使用)// String path = "D://dtimg"; // 实际部署在linux上的真是图片位置。 String path = "/opt/project/face/images/face"; String dirObj = "{"; // 读取指定位置的文件 File file = new File(path); // 读取这个文件夹下的所有文件和文件夹 File[] ss = file.listFiles(); // 对所有的文件夹遍历 for (int i = 0; i < ss.length; i++) { String key = ss[i].getName(); // 文件夹名称 File[] zs = ss[i].listFiles(); // 此文件夹下的文件 // 以文件夹名称为key,拼接成json dirObj += "/"" + key + "/": [" ; for (int j = 0; j < zs.length; j++) { String zn = zs[j].getName(); long size = zs[j].length(); dirObj += "{/"name/":/"" + zn + "/",/"size/":/""+size+"/"},"; } dirObj = dirObj.substring(0, dirObj.length()-1); dirObj += "],"; } dirObj = dirObj.substring(0, dirObj.length()-1); // 注意:json的末尾不能多加逗号(,)。 dirObj += "}"; return dirObj; }}
var xhr = new XMLHttpRequest();xhr.timeout = 16000;xhr.responseType = "text";xhr.open('POST', '/Servlet02/face', true);xhr.onload = function(){ if(this.status == 200||this.status == 304){ var txt = this.responseText; var obj = JSON.parse(txt); console.log(obj); }}xhr.send(null);
这个是主要的代码,涉及到其他功能的代码,还需要在加。关键词:建立