gpt4 book ai didi

java - 如何获取文件夹的内容并将其传递到 Java 服务器页面

转载 作者:行者123 更新时间:2023-12-01 06:11:33 25 4
gpt4 key购买 nike

在 Java Web 应用程序中,如何获取存储在特定文件夹中的所有文件的列表并将它们传递给 JSP?

我尝试执行以下操作:

我将所有文件存储在WebContent下的images文件夹中在 Controller 中,我创建了一个字符串列表,在其中添加了每个文件的 URL Controller 转发到 JSP,在 View 中打印出该文件夹的内容这是我的代码:

Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
File imagesFolder = new File("/uploading images/WebContent/images");
File[] imagesArray = imagesFolder.listFiles();
ArrayList<String> listOfImages = new ArrayList<String>();
for (File file : imagesArray) {
listOfImages.add(file.toString());
}

request.setAttribute("listOfImages", listOfImages);
request.getRequestDispatcher("/elencoImmagini.jsp").forward(request, response);

}

JSP:

<%
ArrayList<String> listOfImages = (ArrayList<String>) request.getAttribute("listOfImages");

for (String image : listOfImages) {
out.println(image);
}

%>

显然我的代码有问题,因为我在 servlet 的这一行收到 NullPointerException:

for (File file : imagesArray) {

最初我以为我向 imagesFolder 传递了无效路径,但我尝试更改它几次,但总是遇到相同的错误。

您认为问题出在哪里?

编辑:这是错误消息:

SEVERE: Servlet.service() for servlet [controller.caricaImmagini] in context with path [/uploading_images] threw exception
java.lang.NullPointerException
at controller.caricaImmagini.doGet(caricaImmagini.java:44)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

最佳答案

在目录路径末尾添加斜杠

File imagesFolder = new File("/uploading images/WebContent/images/");

File 类无法将路径识别为目录,因此无法获取其中的文件。

关于java - 如何获取文件夹的内容并将其传递到 Java 服务器页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33703095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com