spring boot 打包jar后 上传文件 class path resource [] cannot be resolved to URL because it does not exist

在没打包之前我都把用户上传文件保存在classpath路径下的静态文件夹里

path = new File(ResourceUtils.getURL("classpath:").getPath());
File upload = new File(path.getAbsolutePath(), "static/images/goods/");
String uploadPath = upload + File.separator;
filename.transferTo(new File( uploadPath+id+".png"));

这样做在打包之后上传功能就会报异常class path resource [] cannot be resolved to URL because it does not exist

因为打包成jar之后你还向classpath上传文件是不可以的,因为无法对jar进行写入

我的解决办法是做一个自定义静态资源映射,只需要在配置文件中添加自己想要映射的路径即可,我考虑到windows下和Linux下的不同配置了两个

#Windows下的上传静态资源路径
windows.upload.path=D:/uploads/
#linux下的上传静态资源文件路径
linux.upload.path=/uploads/

spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${windows.upload.path},file:${linux.upload.path}

在存储用户上传文件的控制器中

//引入配置文件中自定义的映射路径 
    @Value("${windows.upload.path}")
    private String windowsUploadPath;

    @Value("${linux.upload.path}")
    private String linuxUploadPath;

//方法中
            //判断系统类型
            if (JudgeSystem.isLinux()){
                path = new File(linuxUploadPath);
                upload = new File(path.getAbsolutePath(), "/images/goods/");
            }else if (JudgeSystem.isWindows()){
                path = new File(windowsUploadPath);
                upload = new File(path.getAbsolutePath(), "/images/goods/");
            }
            if (null==upload){
                return reqeustStart.build(500,"上传失败");
            }
            if (!upload.exists()) upload.mkdirs();
            //添加分隔符
            String uploadPath = upload + File.separator;
            filename.transferTo(new File( uploadPath+id+".png"));

访问映射的资源URL为,不需要添uploads

http://你的服务器域名/images/goods/10.png

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误的原因是指定的 class path resource [processes/] 无法解析为 URL,因为它不存在。这个错误通常在上传功能中出现,当打包并上传应用程序时。根据引用,这个错误是由于在打包后,指定的路径无法找到而引起的。为了解决这个问题,可以尝试以下方法: 1. 检查上传路径:确保上传路径的文件夹或目录存在,并且文件夹的权限设置正确。根据引用的解决办法,在配置文件中添加自定义静态资源映射,指定正确的上传路径。根据你的操作系统选择合适的路径配置,比如 Windows 下的上传路径是 D:/uploads/,Linux 下的上传路径是 /uploads/。 2. 检查资源位置配置:确保在配置文件中正确地配置了静态资源位置,包括 classpath、文件路径等。根据引用的配置示例,可以设置 spring.resources.static-locations 的值为 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${windows.upload.path},file:${linux.upload.path}。 3. 检查资源存在性:确保要上传的文件或资源实际上存在于指定的路径中。如果资源不存在,将无法解析为 URL,从而导致这个错误。 总结起来,解决这个错误的关键是确保上传路径和资源位置的设置正确,并且实际的上传文件或资源存在于指定的路径中。通过检查以上几个方面,你应该能够解决 "class path resource [processes/] cannot be resolved to URL because it does not exist" 的错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [class path resource [processes/] cannot be resolved to URL because it does not exist](https://blog.csdn.net/WGH100817/article/details/101719717)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [上传文件 class path resource [] cannot be resolved to URL because it does not exist](https://blog.csdn.net/qq_37975021/article/details/106940168)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值