2022首次更文挑战第28天 | 云盘系统的开发02

127 阅读2分钟

「这是我参与2022首次更文挑战的第28天,活动详情查看:2022首次更文挑战

这一篇我们讲解一下云盘系统中的一些开发功能,比如视频转换流,加密解密方法。

将视频转换为ts流

1、ffmpegPath:视频存放路径,url视频:url,outputPath:视频输出路径 2、FFmpeg是一个完整的,跨平台的解决方案,用于记录,转换和流化音视频,这里调用了getFfmpegCommand方法(根据文件类型设置ffmpeg命令) 3、process输出视频命令

public static Boolean ffmpeg(String ffmpegPath, String url, String outputPath) throws Exception {
     List<String> command = getFfmpegCommand(ffmpegPath, url, outputPath);
     if (null != command && command.size() > 0) {
         return process(command);
     }
     return false;
 }

加密解密

数据安全的问题越来越严重,我们把数据的安全性放在第一位,可以防止黑客入侵拿到数据之后,不知所措。而且泄露之后,也可以保证数据的安全性。 1、Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");得到加密对象Cipher 2、enCipher.init(Cipher.ENCRYPT_MODE, key, iv);设置工作模式为加密模式,给出密钥和向量 3、data.getBytes(StandardCharsets.UTF_8)设置编码格式为UTF-8 4、encodePlus.replace转换换行符,转义符和其他符号 5、encode方法是加密方法,decode方法是解密方法 6、首先替换符合的,再次转换回来data.replace 7、SystemUtil.isWindows()判断是不是windows系统,因为linux和windows系统的换行符是不一样的 8、解密方法和加密方法的处理过程逆序来。

public String encode(String data) throws Exception {        
    Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    enCipher.init(Cipher.ENCRYPT_MODE, key, iv);
    byte[] pasByte = enCipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
    String encodePlus = Base64.getEncoder().encodeToString(pasByte);
    String encodePure = encodePlus.replace("+", "_");
    encodePure = encodePure.replace("/", "-");
    if (SystemUtil.isWindows()) {
        encodePure = encodePure.replace("\r\n", "~");
    } else {
        encodePure = encodePure.replace("\n", "~");
    }

    return encodePure;
}

public String decode(String data) throws Exception {
    data = data.replace("_", "+");
    data = data.replace("-", "/");
    if (SystemUtil.isWindows()) {
        data = data.replace("~", "\r\n");
    } else {
        data = data.replace("~", "\n");
    }
    data = data.replace("~", "\r\n");
    Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    deCipher.init(Cipher.DECRYPT_MODE, key, iv);
    byte[] pasByte = deCipher.doFinal(Base64.getDecoder().decode(data));
    return new String(pasByte, StandardCharsets.UTF_8);
}

删除文件

删除单个文件,fileName:要删除的文件的文件名。个文件删除成功返回true,否则返回false。先判断当前文件路径存在而且只有一个文件,就可以执行删除操作。

public static boolean deleteFile(String fileName) {
    File file = new File(fileName);
    // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
    if (file.exists() && file.isFile()) {
        if (file.delete()) {
            logger.warn("删除单个文件" + fileName + "成功!");
            return true;
        } else {
            logger.warn("删除单个文件" + fileName + "失败!");
            return false;
        }
    } else {
        logger.warn("删除单个文件失败:" + fileName + "不存在!");
        return false;
    }
}