ZQN
2024-06-17 b1ff19545212508d3f65741ab889f0b6df82a511
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.project.common.utils;
 
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
 
import java.io.File;
import java.io.InputStream;
 
/**
 * @Author ckl
 * @Date 2022-09-24 16:41
 */
@Slf4j
public class FileUtil {
    /**
     * linux下运行jar,通过路径获取文件的绝对路径
     * @param path  传相对路径 比如 cert/alipay/xxx.crt
     * @return  返回的路径就是放在linux服务器上的文件路径
     */
    public static String getFileAbsolutePath(String path){
        try {
            // 创建临时文件,获取jar里面的配置文件
            File file = new File("/home/file/" + path);
            if(file.exists()){
                return file.getAbsolutePath();
            }
            InputStream inputStream = null;
            try {
                ClassPathResource resource = new ClassPathResource(path);
                inputStream = resource.getInputStream();
                FileUtils.copyInputStreamToFile(inputStream, file);
                return file.getAbsolutePath();
            } finally {
                IOUtils.closeQuietly(inputStream);
            }
        } catch (Exception e) {
            log.error("FileUtil getFilePath Fail cause by:",e);
        }
        return null;
    }
}