package FileManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class fmServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("GBK");
String web_dir = request.getParameter("dir");
List<String> current_dirs = list_dir(web_dir);
for(String i:current_dirs){
response.getWriter().println(i);
}
}
public static List<String> list_dir(String web_dir) throws IOException {
List<String> listFile = new ArrayList<>();
if(web_dir == null || web_dir.equals("")){ web_dir = "."; }
File file = new File(web_dir);
File[] files = file.listFiles();
if(files != null && files.length >0){
for (File temp: files){
listFile.add(temp.getName() + " || 是否为目录 " + temp.isDirectory() +
" || 是否可读 " + temp.canRead() + " || 是否可写 " + temp.canWrite() +
" || 文件大小 " + temp.length() +
" || 最后修改时间 " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(file.lastModified()));
}
}
return listFile;
}
}
