关于无法将resources的资源打包到class目录

配置资源文件
本文介绍如何在项目中正确配置资源文件,包括将文件设置为资源、修改pom.xml以包含资源目录以及确保resources文件被正确加载的方法。

1、把文件设为resource,
右键目录:
在这里插入图片描述
在这里插入图片描述

2、pom中增加

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**</include>
                </includes>
            </resource>
        </resources>
    </build>

3、pom中删除pom,加了这个pom不会处理resources目录
4、 resources文件名是否正确

在 Spring Boot 项目中,如果你想将项目目录下的 `resources/files/userPic` 路径设置为**静态资源目录**,以便可以通过 URL 直接访问上传的图片(如:`http://localhost:8080/userPic/xxx.png`),你需要通过配置 `WebMvcConfigurer` 来实现。 --- ## ✅ 目标路径 你希望访问: ``` http://localhost:8080/userPic/xxx.png ``` 来加载: ``` src/main/resources/files/userPic/xxx.png ``` --- ## ✅ 配置步骤 ### ✅ 1. 创建目录结构 确保你的项目中存在以下目录: ``` src/main/resources/files/userPic ``` 如果没有,请手动创建。 --- ### ✅ 2. 编写配置类 创建一个配置类,实现 `WebMvcConfigurer` 接口,并重写 `addResourceHandlers` 方法。 ```java import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.nio.file.Path; import java.nio.file.Paths; @Configuration public class StaticResourceConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // 获取 resources/files/userPic 的绝对路径 Path uploadDir = Paths.get("src", "main", "resources", "files", "userPic"); String uploadPath = uploadDir.toAbsolutePath().toString(); // 映射 /userPic/** 到 resources/files/userPic/ registry.addResourceHandler("/userPic/**") .addResourceLocations("file:" + uploadPath + "/"); } } ``` --- ### ✅ 3. 测试访问 启动项目后,将一张图片复制到: ``` src/main/resources/files/userPic/test.png ``` 然后访问: ``` http://localhost:8080/userPic/test.png ``` 你应该能看到这张图片。 --- ## ✅ 补充说明 ### ⚠️ 注意事项: - `file:` 前缀表示这是**文件系统路径**,classpath。 - `src/main/resources` 是开发阶段的资源目录打包成 jar 后,这些资源会被打包 jar 包,能直接写入文件。 - 如果你希望支持**运行时写入**(比如上传图片),建议使用外部目录(如 `userPic/` 放在项目根目录下),而是放在 `resources` 下。 --- ## ✅ 如果你希望运行时上传并写入文件 你可以在项目根目录下创建 `userPic` 文件夹(与 `src` 同级): ``` your-springboot-project/ ├── src/ ├── userPic/ ├── pom.xml └── ... ``` 然后修改配置类: ```java @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { String uploadPath = Paths.get("userPic").toAbsolutePath().toString(); registry.addResourceHandler("/userPic/**") .addResourceLocations("file:" + uploadPath + "/"); } ``` 这样无论你是开发还是打包成 jar,都可以在运行时动态写入文件。 --- ## ✅ 总结对比 | 方式 | 路径 | 优点 | 缺点 | |------|------|------|------| | 放在 `resources/files/userPic` | 开发阶段可用 | 简单 | 打包无法写入 | | 放在项目根目录 `userPic` | 支持运行时写入 | 实用性强 | 需要手动创建目录 | --- ## ✅ 示例:上传接口(写入到运行时目录) ```java import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.nio.file.*; @RestController @RequestMapping("/file") public class FileUploadController { private static final String UPLOAD_DIR = "userPic"; @PostMapping("/upload") public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) { try { // 创建 userPic 文件夹(如果存在) Path uploadPath = Paths.get(UPLOAD_DIR); if (!Files.exists(uploadPath)) { Files.createDirectories(uploadPath); } // 生成新文件名 String originalFilename = file.getOriginalFilename(); String fileExtension = ""; if (originalFilename != null && originalFilename.contains(".")) { fileExtension = originalFilename.substring(originalFilename.lastIndexOf(".")); } String newFilename = System.currentTimeMillis() + "_" + Math.random() + fileExtension; // 保存文件 Path filePath = uploadPath.resolve(newFilename); file.transferTo(filePath); // 返回访问路径 String fileUrl = "/userPic/" + newFilename; return ResponseEntity.ok(fileUrl); } catch (IOException e) { e.printStackTrace(); return ResponseEntity.status(500).body("文件上传失败"); } } } ``` --- ## ✅ 总结流程 | 步骤 | 操作 | |------|------| | 1 | 创建 `resources/files/userPic` 目录(开发阶段) | | 2 | 创建配置类,使用 `addResourceHandler` 映射静态资源 | | 3 | 编写上传接口,保存文件到运行时目录(如 `userPic/`) | | 4 | 浏览器访问 `/userPic/xxx.png` 查看图片 | --- ###
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值