当前位置: 首页 > news >正文

java.net.UnknownHostException: home

原因:springboot访问映射配置问题导致。

原来的映射配置:

@Configuration
public class AppConfig implements WebMvcConfigurer {
@Value("${upload.resource.innerfilepath}")
    private String uploadResourceInnerpath;

    @Value("${upload.resource.innerfilepath.webrelativepath}")
    private String uploadResourceInnerpathRelative;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
            
        //增加视图注册,将配置的指定文件路径,映射到外部网络访问
        String innerPath = "";
        //获取运行jar包所在目录
        ApplicationHome ah = new ApplicationHome(getClass());
        if(null != ah ){
            File jarF = ah.getSource();
            if(null != jarF){
                innerPath = jarF.getParentFile().toString();
                innerPath = FileUtil.windowsPathTransform(innerPath);
            }
        }
        innerPath = innerPath + uploadResourceInnerpath;
       registry.addResourceHandler(uploadResourceInnerpathRelative +"/**").addResourceLocations("file:/"+innerPath);

    }

}

正确的映射配置:

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
            
        //增加视图注册,将配置的指定文件路径,映射到外部网络访问
        String innerPath = "";
        //获取运行jar包所在目录
        ApplicationHome ah = new ApplicationHome(getClass());
        if(null != ah ){
            File jarF = ah.getSource();
            if(null != jarF){
                innerPath = jarF.getParentFile().toString();
                innerPath = FileUtil.windowsPathTransform(innerPath);
            }
        }
        innerPath = innerPath + uploadResourceInnerpath;
        //访问url中uploadResourceInnerpathRelative会映射到innerPath
        //若映射本地文件,则必须是file:/// 为前缀,否则会报错:java.net.UnknownHostException: home
        //或报错:java.net.ConnectException: 拒绝连接 (Connection refused)
        registry.addResourceHandler(uploadResourceInnerpathRelative + "/**").addResourceLocations("file:///"+innerPath);

    }

application.properties

# 本服务内部文件存储路径(与运行的jar包同级目录下upload目录)注意,一定要/结尾
upload.resource.innerfilepath=/uploadResource/
# 相对路径是用于映射网络访问 upload.resource.innerfilepath 目录资源
upload.resource.innerfilepath.webrelativepath=/uploadResource

访问方式:

<img src="/uploadResource/test.png" id="imgUrl" alt="">

1 业务背景

使用springboot搭建的管理平台,在上传文件后需直接网络读取上传的文件(图片)。

使外部网络访问链接读取当前jar所运行的目录同级的目录下资源.

相关文章:

  • 高等数学(第七版)同济大学 习题6-3 个人解答
  • RTC调试
  • 多线程如何使用MDC传递上下文信息
  • HAproxy+Nginx7层负载均衡
  • 算法竞赛进阶指南 货仓选址
  • 报告分享|OpenMediation:2022年年中全球手游市场报告
  • docker容器中elasticsearch配置跨域访问(elasticsearch-head插件访问不到es集群)
  • Echarts 的使用
  • Docker安装部署(详细版)
  • 【react hook】umi获取虚拟dom元素 并点击按钮滚动到对应元素位置
  • list深度拷贝转为JSON字符串,放到Redis
  • java计算机毕业设计社区老人健康服务跟踪系统源码+系统+数据库+lw文档+mybatis+运行部署
  • LeetCode_309_最佳买卖股票时机含冷冻期
  • Vue中使用lottie动画加载json文件
  • 炒股加杠杆方式有哪些
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • Angular 4.x 动态创建组件
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • codis proxy处理流程
  • Cookie 在前端中的实践
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • Hibernate【inverse和cascade属性】知识要点
  • iOS高仿微信项目、阴影圆角渐变色效果、卡片动画、波浪动画、路由框架等源码...
  • PHP那些事儿
  • Protobuf3语言指南
  • React-flux杂记
  • Redis 中的布隆过滤器
  • unity如何实现一个固定宽度的orthagraphic相机
  • 给github项目添加CI badge
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 模型微调
  • 实战|智能家居行业移动应用性能分析
  • 微信小程序:实现悬浮返回和分享按钮
  • ​ ​Redis(五)主从复制:主从模式介绍、配置、拓扑(一主一从结构、一主多从结构、树形主从结构)、原理(复制过程、​​​​​​​数据同步psync)、总结
  • ​ubuntu下安装kvm虚拟机
  • (Redis使用系列) Springboot 使用redis实现接口幂等性拦截 十一
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (附源码)springboot助农电商系统 毕业设计 081919
  • (每日持续更新)jdk api之StringBufferInputStream基础、应用、实战
  • (未解决)macOS matplotlib 中文是方框
  • (一)C语言之入门:使用Visual Studio Community 2022运行hello world
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (一)基于IDEA的JAVA基础10
  • (原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)
  • (转)原始图像数据和PDF中的图像数据
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • .equal()和==的区别 怎样判断字符串为空问题: Illegal invoke-super to void nio.file.AccessDeniedException
  • .gitignore文件设置了忽略但不生效
  • .NET设计模式(2):单件模式(Singleton Pattern)
  • .secret勒索病毒数据恢复|金蝶、用友、管家婆、OA、速达、ERP等软件数据库恢复
  • @cacheable 是否缓存成功_Spring Cache缓存注解
  • [ C++ ] STL---stack与queue
  • [AMQP Connection 127.0.0.1:5672] An unexpected connection driver error occured
  • [Android Pro] android 混淆文件project.properties和proguard-project.txt
  • [Android Studio 权威教程]断点调试和高级调试