springboot operates on local files

Solution 1: Use ResourceUtils

This solution is a trap. For details, please refer to: springboot reads and writes disk json format files.
When using this solution, you will generally be prompted:java.io.FileNotFoundException

Solution 2: Use commons-io

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>
  • Commons-io provides general read and write tool classes for files
  • When reading and writing files, you need to pass in the disk path where the file is located
  • Refer to the following code
File file = new File("E:/data/a.csv");
FileUtils.readFileToString(file, "UTF-8");
  • There is a problem with the above method, that is, the path of the file is hard-coded, which is very error-prone, such as: the computer does not have an E disk at all, how to fix it, what do you think? It's useless...
  • Solution 3 begins to discuss how springboot can obtain the correct file path

Solution 3: springboot obtains the local disk file path

  • Among them, ResourceUtils is a method, use it with caution, the debugging mode is no problem, you can try it in the production environment after the project is released, whoever tries it will know

Solution 1
Use the following code to obtain the path of the Jar package after the project is released. This method is very easy to use, and both the test environment and the production environment are correct.

private String getJarFilePath() {
    
    
    ApplicationHome home = new ApplicationHome(getClass());
    File jarFile = home.getSource();
    return jarFile.getParentFile().toString();
}

Solution 2
There will be problems in this method. The debugging environment can get the disk path where the project is located, but when running the java -jar E:\project.1.0.0.JarJar package with the command, it will not get the correct path of the Jar package, but will get the path when running the java command

System.getProperty("user.dir")

Solution 3
Get the absolute path of the classes directory

String path = ResourceUtils.getURL("classpath:xxx").getPath();
String path = ClassUtils.getDefaultClassLoader().getResource("xxx").getPath();

Using the above method, if there is Chinese in the project path, the file path will have garbled characters, as follows:
E:/%e5%ae%89%e6%a3%80%e8%81%94%e7%bd%91%e5% 8d%87%e7%ba%a7%e6%94%b9%e9%80%a0/usb-key/target/classes/sysconfig.json

what is classpath

  • In a Maven project, all resources files will be copied to the classes directory. The classpath in the tomcat project is /classes, /lib and other paths under tomcat.
  • For developers, generally the directory where the classes are located is the starting point and base path of the classpath.

Solution 4
Use the following conventional method, and there will be problems when it is packaged into a Jar package

File file = new File("src/main/resources/static/assets/test.txt");
InputStream inputStream=new FileInputStream(file);

Solution 4: Read the resources in the Jar package through ResourceLoader using file stream

The file read by ResourceLoader is a stream, so the stream is further converted into a file for easy operation

   ResourceLoader resourceLoader = new DefaultResourceLoader();
   InputStream inputStream = resourceLoader.getResource("static/img/logo.png").getInputStream();
   //通过将文件转换为临时文件进行操作
   File file = File.createTempFile("logo", ".png");
   try {
    
    
        FileUtils.copyInputStreamToFile(inputStream, file);
   } finally {
    
    
   		//关闭IO
        IOUtils.closeQuietly(inputStream);
   }

Summarize

If you want to read the resource files in the Jar package, you must use the file stream method

ClassPathResource classPathResource = new ClassPathResource("static/assets/test.txt");
InputStream inputStream = classPathResource.getInputStream();

You can also refer to the article: Java getResourceAsStream() to obtain the resource file in the Jar package.
The editor did not write a specific implementation, you need to complete it yourself, hehe

Summary

If it is not a special case, I hope you will still use the solution of Solution 2 combined with Solution 3 to deal with the problem of reading and writing files (put some files in the disk file directory at the same level as Jar), ​​and do not easily operate the Jar package. After all, the Jar package has already been packed.

Guess you like

Origin blog.csdn.net/baidu_38493460/article/details/128660849