获取目录内所有文件名字列表,可供下载等
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//定义一个存储文件名的集合list
List<String> fileNames = new ArrayList<String>();
//获得存储文件的绝对路径
String path = request.getSession().getServletContext().getRealPath("/upload/");
//创建文件对象
File file = new File(path);
if(file.isDirectory()){
//获得目录下所有文件
File[] files = file.listFiles();
for(File fi : files){
fileNames.add(fi.getName());
}
}
request.setAttribute("downfiles", fileNames);
request.getRequestDispatcher("/down.jsp").forward(request, response);
}