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; } }