[英]can't access localhost:8080 public folder from browser (java spring boot)
i have a java spring boot application that have functionality to save files in "src/main/resources/public/myFiles" path.我有一个 java spring 启动应用程序,它具有将文件保存在“src/main/resources/public/myFiles”路径中的功能。 Now i want to access files and i tried to access the file by typing " http://localhost:8080/public/myFiles " and i'm getting an error saying below.
现在我想访问文件,我尝试通过键入“ http://localhost:8080/public/myFiles ”来访问该文件,但我收到以下错误消息。
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
Whitelabel 错误页面 此应用程序没有针对 /error 的显式映射,因此您将其视为后备。
There was an unexpected error (type=Not Found, status=404).
出现意外错误(类型=未找到,状态=404)。 No message available
没有可用的消息
I think you have to add the location to your static folder in the application.properties我认为您必须将位置添加到 application.properties 中的 static 文件夹
spring.resources.static-locations=file:<location>
And also to access the file don't give the static-locations
folder name.并且访问该文件也不要给出
static-locations
文件夹名称。 you can access it directly if it is not in a subfolder.如果它不在子文件夹中,您可以直接访问它。 like this
像这样
http://localhost:8080/filename.ext http://localhost:8080/filename.ext
You cannot access your file if it's not on your static folder location, otherwise you need to specify the exact path and file name to access it.如果文件不在 static 文件夹位置,则无法访问您的文件,否则您需要指定准确的路径和文件名才能访问它。
So I have been making a file sharing web app using Spring Boot and here is how I have facilitated getting files from the database.因此,我一直在使用 Spring Boot 制作文件共享 web 应用程序,这就是我如何帮助从数据库中获取文件。 I will focus specifically on your problem because it is very similar to what I faced a few days ago.
我将特别关注您的问题,因为它与我几天前遇到的问题非常相似。
Firstly, I make a POJO class Files
which has details like filename
, lastmodified
, and some others which are not relevant to your problem here.首先,我制作了一个 POJO class
Files
,其中包含filename
、 lastmodified
等与您的问题无关的其他详细信息。
I get the files from the database using a DAO class, add the above mentioned attributes in an ArrayList and return the ArrayList back to the Controller class. I get the files from the database using a DAO class, add the above mentioned attributes in an ArrayList and return the ArrayList back to the Controller class. In the Controller class, using
request
object I do the following: request.setAttribute("files", files);
在 Controller class 中,使用
request
object 我执行以下操作: request.setAttribute("files", files);
Now in my jsp file, I use JSTL to get files like this:现在在我的 jsp 文件中,我使用 JSTL 来获取如下文件:
<c:forEach var="j" items="${files}">
<td>
<a href="resources/files/${j.getFilename()}" target="_blank" rel="noopener noreferrer">${j.getFile()}</a>
</td>
</c:forEach>
resources/files/
is the path where my files are stored after being uploaded and using target="_blank" rel="noopener noreferrer"
enables me to open files (files which the browser allows to be opened such as pdf and text files) in a new tab and those which cannot be opened get automatically downloaded. resources/files/
是我的文件上传后存储的路径,使用target="_blank" rel="noopener noreferrer"
使我能够在一个新选项卡和那些无法打开的选项卡会自动下载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.