[英]Reading file inside Spring boot fat jar
我们有一个 spring boot 应用程序,它有一个遗留的 jar api,我们使用它需要使用 InputFileStream 加载属性。 我们将遗留 jar 包在我们的 spring boot fat jar 中,属性文件位于 BOOT-INF/classes 文件夹下。 我可以看到 spring 加载了所有相关的属性,但是当我将属性文件名传递给遗留 jar 时,它无法读取属性文件作为它在 jar 内部并且不在物理路径下。 在这种情况下,我们如何将属性文件传递给遗留 jar?
请注意,我们无法更改旧 jar。
我最近也遇到了这个问题。 我有一个文件放在资源文件中,我们称之为path
,但我无法读取它。 @madoke 给出了使用 FileSystem 的解决方案之一。 这是另一个,这里我们假设我们在一个容器中,如果不是,我们使用 Java 8 特性。
@Component
@Log4j2
public class DataInitializer implements
ApplicationListener<ApplicationReadyEvent> {
private YourRepo yourRepo; // this is your DataSource Repo
@Value(“${path.to.file}”). // the path to file MUST start with a forward slash: /iaka/myfile.csv
private String path;
public DataInitializer(YourRepo yourRepo) {
this.yourRepo = yourRepo;
}
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
try {
persistInputData();
log.info("Completed loading data");
} catch (IOException e) {
log.error("Error in reading / parsing CSV", e);
}
}
private void persistInputData() throws IOException {
log.info("The path to Customers: "+ path);
Stream<String> lines;
InputStream inputStream = DataInitializer.class.getClassLoader().getResourceAsStream(path);
if (inputStream != null) { // this means you are inside a fat-jar / container
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
lines = bufferedReader.lines();
} else {
URL resource = this.getClass().getResource(path);
String path = resource.getPath();
lines = Files.lines(Paths.get(path));
}
List<SnapCsvInput> inputs = lines.map(s -> s.split(","))
.skip(1) //the column names
.map(SnapCsvInput::new)
.collect(Collectors.toList());
inputs.forEach(i->log.info(i.toString()));
yourRepo.saveAll(inputs);
}
}
其实你可以,使用FileSystem 。 您只需要在需要从中获取文件的文件夹上模拟文件系统。 例如,如果您想获取src/main/resources
下的file.properties
,您可以执行以下操作:
FileSystem fs = FileSystems.newFileSystem(this.getClass().getResource("").toURI(), Collections.emptyMap());
String pathToMyFile = fs.getPath("file.properties").toString();
yourLegacyClassThatNeedsAndAbsoluteFilePath.execute(pathToMyFile)
基本上,你不能,因为属性“文件”不是一个文件,它是一个 jar 文件中的资源,它被压缩(它实际上是一个 .zip 文件)。
AFAIK,使这项工作的唯一方法是从 jar 中提取文件,并将其放在您的服务器上的一个众所周知的位置。 然后在运行时使用 FileInputStream 打开该文件并将其传递给遗留方法。
我想从resources文件夹中读取一个JSON文件。src / main / resources因此在下面编写了类似这样的代码。
Spring Boot Reading Spring Boot中的参考资源不适用于胖子。 在花了很多时间处理这些简单的事情之后,我发现下面的工作。
如果发胖了,我在Spring Boot中找到了阅读资源的详细链接 。
@Value("classpath:test.json")
Resource resourceFile;
try {
InputStream resourcee =resource.getInputStream();
String text = null;
try (final Reader reader = new InputStreamReader(resourcee)) {
text = CharStreams.toString(reader);
}
System.out.println(text);
} catch (IOException e) {
e.printStackTrace();
}
根据@moldovean 的解决方案,您只需要调用“getResourceAsStream(path)”并继续返回的InputStream。 请注意,“路径”基于 ClassPath。 例如:
InputStream stream = this.getClass().getResourceAsStream("/your-file.csv");
在类路径的根目录中,有一个名为“your-file.csv”的文件要读取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.