Spring boot 权限配置文件引入坑

背景:权限资源Jar包打包不能,直接进行路径读取

解决方案
将资源文件打包后可以获得文件流信息,将文件流保存成文件,再引入

注意点:
1.文件打包, 资源文件放入resource里
pom.xml


        
            
             
                src/main/resources
                true
            
        
        
            
                org.apache.maven.plugins
                maven-resources-plugin
                3.0.2
                UTF-8
                    
                    
                        crt
                        p12
                    
                
            
        
    

流文件拷贝, 外部直接使用

 ClassPathResource resourceCa = new ClassPathResource("文件名");
 InputStream inputStreamCa = resourceCa.getInputStream();
  String path = inputStreamToFile(inputStreamCa, "保存文件名");

public static String inputStreamToFile(InputStream inputStream, String targetFile) {
        OutputStream outputStream = null;
        try {
            ApplicationHome h = new ApplicationHome(EsClient.class);
            File jarF = h.getSource();
            File file = new File(jarF.getParentFile() + "/"+ targetFile);
            file.mkdirs();
            if(file.exists()){
                file.delete();
            }
            outputStream = new FileOutputStream(file);
            int byteCount = 0;
            byte[] bytes = new byte[1024];
            while ((byteCount = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, byteCount);
            }
            log.debug("jarpath:" + file.getAbsolutePath());
            return file.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

你可能感兴趣的:(Spring boot 权限配置文件引入坑)