遍历系统目录,以JSON格式响应请求

由于用到gson,需要导入gson库。首先封装model类:

importjava.util.List;

public class FileModel {

    private String name;

    private String type;

    private String path;

private List children;

public FileModel(String name, String type, String path) {

super();

this.name = name;

this.type = type;

this.path = path;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public String getPath() {

return path;

}

public void setPath(String path) {

this.path = path;

}

public List getChildren() {

return children;

}

public void setChildren(List children) {

this.children = children;

}

}


编写servlet,实现功能

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FilenameFilter;

importjava.io.IOException;

importjava.io.PrintWriter;

importjava.util.ArrayList;

importjava.util.List;

importjavax.servlet.ServletException;

importjavax.servlet.annotation.WebServlet;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importcom.google.gson.Gson;

@WebServlet("/getFiles")

public class ListFileController extends HttpServlet {

private static final long serialVersionUID = 1L;

private Gson gson = new Gson();

private String rootFile = "/Users/terry/Books";

    public ListFileController() {

        super();

    }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

rootFile = request.getServletContext().getRealPath(".");

        response.setHeader("content-type","application/json;charset=UTF-8");

        response.setCharacterEncoding("UTF-8");

        PrintWriter out = response.getWriter();

File file = new File(rootFile);

List fileModel = getFileList(file);

        StringjsonString=gson.toJson(fileModel);

        out.println(jsonString);

        out.flush();

        out.close();

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}

private final FilenameFilter FILE_ILTER = new FilenameFilter() {

@Override

public boolean accept(File dir, String name) {

File acceptFile = new File(dir, name);

boolean result = acceptFile.isDirectory() || "%PDF".equalsIgnoreCase(getFileHeader(acceptFile));

return result;

}

};

final String getFileHeader(final File file) {

FileInputStream is = null;

String value = null;

try {

is = new FileInputStream(file);

byte[] b = new byte[4];

is.read(b, 0, b.length);

value=newString(b);

} catch (Exception e) {

} finally {

if(null!=is){

try{

is.close();

} catch (IOException e) {

}

}

}

return value;

}

List getFileList(final File startFileDir) {

List result = null;

synchronized (this) {

result = new ArrayList<>();

File[] fileDirs = startFileDir.listFiles(FILE_ILTER);

for (File file : fileDirs) {

FileModel fileModel = new FileModel(file.getName(), file.isFile()?"2":"1",file.getPath().replace(rootFile, ""));

result.add(fileModel);

if (file.isDirectory()) {

List deeperList = getFileList(file);

if(deeperList!=null && !deeperList.isEmpty()) {

fileModel.setChildren(deeperList);

}

}

}

}

return result;

}

}

你可能感兴趣的:(遍历系统目录,以JSON格式响应请求)