You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC Controller中如何用相对路径创建resources/java.ini文件

在Spring MVC中写入resources目录下文件的正确姿势

首先得给你提个醒:直接往classpath下的resources目录写入文件在生产环境是不可行的——因为当项目打包成JAR/WAR后,resources里的文件是嵌入在包内部的,属于只读资源,没法直接修改或写入。下面分两种场景给你解决方案:

1. 开发环境临时方案(仅适用于IDE运行时)

如果你只是在本地开发测试时需要写入resources目录,可以通过Spring的ResourceLoader获取resources目录的真实磁盘路径:

import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.RestController;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import java.nio.file.StandardOpenOption;
import java.io.IOException;

@RestController
public class FileController {

    @Autowired
    private ResourceLoader resourceLoader;

    public void writeToIniFile(byte[] data) {
        try {
            // 获取classpath下的java.ini资源
            Resource resource = resourceLoader.getResource("classpath:java.ini");
            Path filePath = Paths.get(resource.getURI());
            
            // 写入文件
            Files.write(filePath, data, StandardCharsets.UTF_8, StandardOpenOption.CREATE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这个方法在IDE里运行时能正常工作,因为此时resources是磁盘上的真实目录;但打包成JAR后会报错,因为resource.getURI()会指向JAR内部的路径,无法写入。

2. 生产环境通用方案(推荐)

正确的做法是将文件写入项目外部的可写目录,比如项目根目录下的自定义文件夹,或者通过配置指定路径:

方式一:写入项目运行根目录下的文件夹

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import java.nio.file.StandardOpenOption;
import java.io.IOException;

@RestController
public class FileController {

    public void writeToIniFile(byte[] data) {
        try {
            // 获取项目运行的根目录
            String projectRoot = System.getProperty("user.dir");
            // 构建文件路径:项目根目录/data/java.ini
            Path filePath = Paths.get(projectRoot, "data", "java.ini");
            
            // 确保父目录存在(如果不存在则创建)
            Files.createDirectories(filePath.getParent());
            
            // 写入文件
            Files.write(filePath, data, StandardCharsets.UTF_8, StandardOpenOption.CREATE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

方式二:通过配置文件指定路径(更灵活)

  1. 在application.properties(或application.yml)中配置存储路径:
# 自定义文件存储路径,可根据需求修改
file.storage.path=./application-data
  1. 在Controller中注入配置并使用:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import java.nio.file.StandardOpenOption;
import java.io.IOException;

@RestController
public class FileController {

    @Value("${file.storage.path}")
    private String storagePath;

    public void writeToIniFile(byte[] data) {
        try {
            Path filePath = Paths.get(storagePath, "java.ini");
            Files.createDirectories(filePath.getParent());
            Files.write(filePath, data, StandardCharsets.UTF_8, StandardOpenOption.CREATE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

关键总结

  • 永远不要试图往classpath的resources目录写入文件(生产环境不可行)
  • 开发环境可以用ResourceLoader临时实现,但仅限测试场景
  • 生产环境一定要用外部可写目录,通过配置指定路径更便于维护

内容的提问来源于stack exchange,提问作者user9541758

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:51:22