• 从jar包内复制文件到系统目录内


    需求
    最近想实现一个java执行shell脚本的小demo,其实执行的一条命令是比较容易的,一百度一大堆;执行脚本也特别简单,参数为路径即可,但是有没有想过下面的这种情况?

    你想执行一个名字叫 helloword.sh脚本,你的脚本放在 /opt下,你在自己的SpringBoot代码运行shell的命令的参数为/opt/ helloword.sh ,你的代码在自己的服务上跑的美滋滋,但是你的代码跑到别人服务器上,那你怎么能保证别人的/opt下有你的helloword.sh ???

    其实比较简单的方法就是我在的SpringBoot的resources目录下放置helloworld.sh,如果jar包启动的时候,能把该helloword.sh复制到当前linux操作系统我规定的目录下,我这个问题就解决了。

    代码实现
    源码
    https://github.com/cbeann/Demooo/tree/master/springboot-demo-copy

    项目介绍
    SpringBoot项目,secret.txt文件的位置如下图所示

    核心代码
    pom.xml

    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.8.0</version>
    </dependency>
    runner:项目启动后调用实现CommandLineRunner接口的实现类

    package com.example.springbootdemocopy.runner;

    import com.example.springbootdemocopy.App;
    import org.apache.commons.io.FileUtils;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.stereotype.Component;

    import java.io.File;
    import java.io.InputStream;

    /**
    * @author chaird
    * @create 2020-10-12 20:41
    */
    @Component
    public class CopyRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {

    // window();//window上复制文件
    linux(); // linux上复制文件
    }
    /** 在window系统上把resources下的myfile/secret.txt文件复制到xxx */
    public void linux() throws Exception {

    InputStream inputStream = App.class.getClassLoader().getResourceAsStream("myfile/secret.txt");

    // 获得的系统的根目录
    File fileParent = new File(File.separator);
    /// opt/secret_linux.txt
    File targetFile = new File(fileParent, "/opt/secret_linux.txt");

    if (targetFile.exists()) {
    targetFile.delete();
    }
    targetFile.createNewFile();

    // 使用common-io的工具类即可转换
    FileUtils.copyToFile(inputStream, targetFile);

    // 记得关闭流
    inputStream.close();
    }

    /** 在window系统上把resources下的myfile/secret.txt文件复制到D:others emp empsecret_win.txt目录下 */
    public void window() throws Exception {

    String sourceFileName = "myfile/secret.txt";

    ClassPathResource classPathResource = new ClassPathResource(sourceFileName);
    InputStream inputStream = classPathResource.getInputStream();

    String targetFileName = "D:\others\temp\temp\secret_win.txt";

    // 获得的系统的根目录
    File targetFile = new File(targetFileName);

    if (targetFile.exists()) {
    targetFile.delete();
    }
    targetFile.createNewFile();

    // 使用common-io的工具类即可转换
    FileUtils.copyToFile(inputStream, targetFile);

    // 记得关闭流
    inputStream.close();
    }
    }

    参考

    ————————————————
    版权声明:本文为CSDN博主「CBeann」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_37171353/article/details/109036272

  • 相关阅读:
    MySQL query_cache_type 详解
    MySQL validate_password 插件
    MySQL冷备份的跨操作系统还原
    MySQL5.7新特性笔记
    MySQL参数详解
    保存mysql用户的登录信息到~.my.cnf文件;用于方便登录操作。
    MySQL应用层传输协议分析
    python egg
    MySQL 加锁处理分析
    train_test_split, 关于随机抽样和分层抽样
  • 原文地址:https://www.cnblogs.com/javalinux/p/15020793.html
Copyright © 2020-2023  润新知