Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to get static Resource path in springboot

Shulou Source: shulou.com Published: 2022-06-01 16:45:03 09月22日 Update

This article is about how to get static resource paths in springboot. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

Method one: String path = ClassUtils.getDefaultClassLoader (). GetResource ("). GetPath ()

String filePath = path + "static/ payment platform unit .xlsx"

The filePath you get is the corresponding static directory

Method 2:

Get it directly without configuring a static directory

Found that there is an advantage, compared to the previous approach, this method will not fail on linux or jar!

ClassPathResource classPathResource = new ClassPathResource ("static/ payment platform unit .xlsx")

InputStream inputStream = classPathResource.getInputStream ()

Ps: it is recommended to use the two-way writing method, and the personal test is better than method one.

Expand:

If you want to access the static file directly on the browser through the address + file name

For example: localhost:9090/static/ payment platform unit .xlsx

Localhost:9090/static/index.html, wait.

You need to set a readable static directory in the configuration file. Keep in mind that the path to the open directory is as specific as possible to prevent static resources from leaking.

First, set the static resource access address of the springboot project:

Set the readable static directory (application.yaml) in the configuration file

Spring:

Mvc:

Static-path-pattern: / static/**

The above is how to get the static resource path in springboot, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Static mode directory resource file path unit platform payment configuration address that is more knowledge article central practical writing benefits work meeting Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Information MySQL Shulou Tech Info Microsoft