简体   繁体   English

无法从浏览器访问 localhost:8080 公用文件夹(java spring 启动)

[英]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 ,其中包含filenamelastmodified等与您的问题无关的其他详细信息。

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.

相关问题 带 Angular 的弹簧靴。 CORS 策略已阻止从源“http://localhost:4200”访问“http://localhost:8080/”处的 XMLHttpRequest - Spring boot with Angular. Access to XMLHttpRequest at 'http://localhost:8080/' from origin 'http://localhost:4200' has been blocked by CORS policy 运行 Spring Boot JAR 应用程序不会使用浏览器打开 URL(https://localhost:8080/) - Running a Spring Boot JAR Application does not open a URL(https://localhost:8080/) with a browser Spring Boot2 Oauth2隐式流 - http:// localhost:8080 / oauth / authorize获取访问被拒绝 - Spring Boot2 Oauth2 Implicit Flow - http://localhost:8080/oauth/authorize getting Access Denied Spring Boot 无法访问本地主机 Mongodb - Spring Boot can't reach localhost Mongodb Java Spring Boot-路由访问静态文件夹 - Java Spring Boot - routing for access to static folder Spring 无法从浏览器访问启动应用程序,也无法访问 Postman - Spring Boot application can't be reached from browser nor Postman spring 引导项目的 html 网站中的图片未显示在 localhost:8080 - Pictures in an html website of a spring Boot project arent shown on localhost:8080 春季靴子| 本地主机:8080 404错误页面显示 - Spring Boot | localhost: 8080 404 error page displayed 使用基本目录运行 Spring Boot 项目 - localhost:8080/demo/ - Run Spring Boot Project with base directory - localhost:8080/demo/ Spring Boot 2.0.4 + OAuth2 + JWT-无法获取访问令牌,返回405或仅被映射到localhost:8080 / - Spring Boot 2.0.4 + OAuth2 + JWT - Cannot get Access Token, returns 405 or just gets mapped into localhost:8080/
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM