我正在尝试显示本地目录中的图像列表,但未显示任何内容。我收到该目录为空的错误异常处理消息。我实际上在目录中没有几个图像。我不知道我在做什么错。
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List imgUrlList = new ArrayList();
File imageDir = new File("/images");
File[] files=imageDir.listFiles();
if(files!=null) {
for (File imageFile : files) {
String imageFileName = imageFile.getName();
imgUrlList.add(imageFileName);
}
}
else {
System.out.println("directory is empty");
}
req.setAttribute("imgUrlList", imgUrlList);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/test/test.jsp");
dispatcher.forward(req, resp);
}