multimedia-utils-一款java后端的图片、视频处理工具jar包

2021SC@SDUSC

RuntimeUtils

博客十

获取Runtime类对象:public static Runtime getRuntime()

获取最大可用内存:public long maxMemory()
获取总共可用内存:public long totalMemory()
获取空闲内存:public long freeMemory()

相关代码段

package com.whty.zdxt.multimedia.util;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
/**
 * @author GuoNanLin
 * @date 2020-10-30
 */
class RuntimeUtils {
    private static final Log log = LogFactory.getLog(RuntimeUtils.class);
 
    /**
     * 执行cmd命令,打印成功失败信息
     */
    public static void execSF(String command) {
        log.debug("执行命令:" + command);
        Process exec = null;
        try {
            exec = Runtime.getRuntime().exec(command);
            exec.waitFor();
            RuntimeUtils.successful(exec);
            String failure = RuntimeUtils.failure(exec);
            int i = exec.exitValue();
            if (i != 0) {
                throw new RuntimeException(failure);
            }
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException("执行命令失败", e);
        } finally {
            if (exec != null) {
                exec.destroy();
            }
        }
    }
 
    /**
     * 执行cmd命令,打印失败信息
     */
    public static void execFailure(String command) {
        log.debug("执行命令:" + command);
        Process exec = null;
        try {
            exec = Runtime.getRuntime().exec(command);
            exec.waitFor();
            String failure = RuntimeUtils.failure(exec);
            int i = exec.exitValue();
            if (i != 0) {
                throw new RuntimeException(failure);
            }
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException("执行命令失败", e);
        } finally {
            if (exec != null) {
                exec.destroy();
            }
        }
    }
 
    /**
     * 执行cmd命令,打印失败信息
     */
    public static String execSuccessful(String command) {
        log.debug("执行命令:" + command);
        Process exec = null;
        String response = null;
        try {
            exec = Runtime.getRuntime().exec(command);
            response = RuntimeUtils.successful(exec);
        } catch (IOException e) {
            throw new RuntimeException("执行命令失败", e);
        } finally {
            if (exec != null) {
                exec.destroy();
            }
        }
        return response;
    }
 
    /**
     * 获取执行成功结果
     */
    private static String successful(Process exec) throws IOException {
        InputStream is = exec.getInputStream();
        return response(is);
    }
 
    /**
     * 获取执行失败结果
     */
    private static String failure(Process exec) throws IOException {
        InputStream is = exec.getErrorStream();
        return response(is);
    }
 
    /**
     * 打印执行结果
     *
     * @param is 结果输入流
     */
    private static String response(InputStream is) throws IOException {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        String r = "";
        while ((line = br.readLine()) != null) {
//            System.out.println(line);
            r = r + line;
        }
        return r;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值