千家信息网

SpringBoot如何使用项目外部路径图片以及解决CORS跨域问题

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,本篇文章给大家分享的是有关SpringBoot如何使用项目外部路径图片以及解决CORS跨域问题,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看
千家信息网最后更新 2026年10月31日SpringBoot如何使用项目外部路径图片以及解决CORS跨域问题

本篇文章给大家分享的是有关SpringBoot如何使用项目外部路径图片以及解决CORS跨域问题,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

配置类
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
配置文件设置外面路径地址
application.properties
## 图片上传真是地址
imagesPath=file:/C:/upload/
12
测试 访问图片路径:http://localhost:9001/core/images/2.jpg
----------------
版权声明:本文为CSDN博主「bseayin」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/h456363/article/details/90547998

图片实际存放路径:C:\upload

SpringBoot2.X
Springboot2 里面WebMvcConfigurerAdapter 已经过时。
推荐使用接口 WebMvcConfigurer 。
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 统一处理
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类作用描述
* @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("*");//允许域名访问,如果*,代表所有域名
}
};
}
}




以上就是SpringBoot如何使用项目外部路径图片以及解决CORS跨域问题,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。

图片 路径 问题 图片以及 项目 原文 地址 域名 文章 更多 版权 知识 篇文章 链接 配置 实用 过时 代表 作用 出处 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 蓝思科技飞鸽互联网 新华互联网科技学校 厦门中拓互联网络科技有限公司 互联网科技公司怎么样 礼当家互联网科技 互联网与新科技 安徽鑫晟辉互联网科技 互联网科技logo 互联网科技公司做什么 科技伦理 互联网公司 互联网科技产品 港股科技和互联网 荆州市昕露互联网络科技有限公司 青岛科技大学互联网大赛研究生 工业互联网等科技实现智能化赋能 深圳无忧互联网科技 互联网科技新闻 张家口科技产业互联网平台 杭州科技互联网公司有哪些 互联网领先科技成果宣传视频 科技互联网公司装修 关于新基建和互联网科技 互联网金融和金融科技 畅思互联网科技 锡山区互联网智能科技产品供应 从那以后互联网科技有限公司 卡尔迅互联网科技有限公司 西安有哪些互联网高科技企业 昆明中奇联合互联网科技 上海万顺科技互联网有限公司招聘
0