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所运行的目录同级的目录下资源.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
java.net.UnknownHostException异常是Java编程中经常遇到的网络错误之一,该异常在建立网络连接时通常发生,原因是程序无法解析所查询的主机名,即无法找到该主机名对应的IP地址。对于出现java.net.UnknownHostException异常,我们一般需要检查其可能的原因和解决方案。 在本问题中,出现了java.net.UnknownHostException: openapi.alipay.com异常,这是因为程序无法解析openapi.alipay.com这个主机名。针对这个问题,我们需要排查如下几个可能的原因: 1. DNS解析问题:程序可能没有正确配置或无法访问DNS服务器,导致无法解析域名为IP地址,需要确认DNS解析是否正常。 2. 网络连接问题:可能存在网络连接问题,比如代理服务器配置不正确、网络不稳定等问题,需要确认网络连接是否正常。 3. 安全相关问题:如果运行程序需要进行安全认证,可能是证书相关问题导致无法建立连接,需要检查证书是否正确。 针对以上可能的原因,我们可以尝试相应的解决方案: 1. DNS解析问题可以尝试更换DNS服务器、在hosts文件中手动添加IP地址等方式解决。 2. 网络连接问题可以尝试更换网络环境,检查代理服务器等。 3. 安全相关问题需要确认证书的正确性,可以参考相应的API文档进行调试和排查。 总之,出现java.net.UnknownHostException异常,需要认真分析可能出现的原因和解决方案,通过正确的调试和排查,才能最终解决问题。针对本问题,需要检查网络连接、DNS解析等方面,如果还不能解决问题,可以查阅相关API文档进行更深入的调试和排查。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MarshalEagle

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值