How to use project external path picture and solve CORS cross-domain problem by SpringBoot
This article is about how SpringBoot uses the external path pictures of the project and how to solve the cross-domain problem of CORS. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Configuration class
Package com.zz.config
Import org.springframework.beans.factory.annotation.Value
Import org.springframework.context.annotation.Configuration
Import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
Import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
@ Configuration
Public class WebAppConfig extends WebMvcConfigurerAdapter {
@ Value ("${imagesPath}")
Private String mImagesPath
@ Override
Public void addResourceHandlers (ResourceHandlerRegistry registry) {
If (mImagesPath.equals (") | | mImagesPath.equals (" ${imagesPath} ")) {
String imagesPath = WebAppConfig.class.getClassLoader () .getResource (") .getPath ()
If (imagesPath.indexOf (".jar") > 0) {
ImagesPath = imagesPath.substring (0, imagesPath.indexOf (".jar"))
} else if (imagesPath.indexOf ("classes") > 0) {
ImagesPath = "file:" + imagesPath.substring (0, imagesPath.indexOf ("classes"))
}
ImagesPath = imagesPath.substring (0, imagesPath.lastIndexOf ("/")) + "/ images/"
MImagesPath = imagesPath
}
/ / LoggerFactory.getLogger (WebAppConfig.class) .info ("imagesPath=" + mImagesPath)
Registry.addResourceHandler ("/ images/**") .addResourceLocations (mImagesPath)
/ / TODO Auto-generated method stub
Super.addResourceHandlers (registry)
}
}
12345678910111213141516171819202122232425262728293031
Configuration file sets the outer path address
Application.properties
# # Fax on the picture is the address
ImagesPath=file:/C:/upload/
twelve
Test access image path: http://localhost:9001/core/images/2.jpg
-
Copyright notice: this article is the original article of CSDN blogger "bseayin", in accordance with the CC 4.0BY-SA copyright Agreement. Please attach the original source link and this statement for reprint.
Original link: https://blog.csdn.net/h456363/article/details/90547998
The actual storage path of the picture: C:\ upload
SpringBoot2.X
WebMvcConfigurerAdapter in Springboot2 is out of date.
Interface WebMvcConfigurer is recommended.
Package com.cy.config
Import org.springframework.beans.factory.annotation.Value
Import org.springframework.context.annotation.Configuration
Import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
Import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
@ Configuration
Public class WebAppConfig implements WebMvcConfigurer {
@ Value ("${imagesPath}")
Private String mImagesPath
@ Override
Public void addResourceHandlers (ResourceHandlerRegistry registry) {
If (mImagesPath.equals (") | | mImagesPath.equals (" ${imagesPath} ")) {
String imagesPath = WebAppConfig.class.getClassLoader () .getResource (") .getPath ()
If (imagesPath.indexOf (".jar") > 0) {
ImagesPath = imagesPath.substring (0, imagesPath.indexOf (".jar"))
} else if (imagesPath.indexOf ("classes") > 0) {
ImagesPath = "file:" + imagesPath.substring (0, imagesPath.indexOf ("classes"))
}
ImagesPath = imagesPath.substring (0, imagesPath.lastIndexOf ("/")) + "/ images/"
MImagesPath = imagesPath
}
/ / LoggerFactory.getLogger (WebAppConfig.class) .info ("imagesPath=" + mImagesPath)
Registry.addResourceHandler ("/ images/**") .addResourceLocations (mImagesPath)
/ / TODO Auto-generated method stub
}
}
12345678910111213141516171819202122232425262728293031
CORS unified treatment of cross-domain problems
Package com.zz.config
Import org.springframework.context.annotation.Bean
Import org.springframework.context.annotation.Configuration
Import org.springframework.web.servlet.config.annotation.CorsRegistry
Import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
/ * *
* @ Description: java class function description
* @ Author: Bsea
* @ CreateDate: 2019-10-9 $21:33 $
, /
@ Configuration
Public class CorsConfig {
@ Bean
Public WebMvcConfigurer corsConfigurer () {
Return new WebMvcConfigurer () {
@ Override
Public void addCorsMappings (CorsRegistry registry) {
Registry.addMapping ("/ *")
.allowedOrigins ("*"); / / allow domain name access. If *, it represents all domain names
}
}
}
}
The above is how SpringBoot uses project external path pictures and solves CORS cross-domain problems. 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.