前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >从jar包内复制文件到系统目录内

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

作者头像
CBeann
发布2023-12-25 18:37:56
1530
发布2023-12-25 18:37:56
举报
文章被收录于专栏:CBeann的博客CBeann的博客

需求

最近想实现一个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

代码语言:javascript
复制
 <!-- 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接口的实现类

代码语言:javascript
复制
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\temp\temp\secret_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();
  }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-03-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 需求
  • 代码实现
    • 源码
      • 项目介绍
        • 核心代码
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档