Java将C:/video.mp4转为byte[],再将byte[]转为视频到C:/temp/1/video.mp4

原始视频C:/video.mp4

在这里插入图片描述

现在视频C:/temp/1/10位uuid/Tom.mp4在这里插入图片描述
package org.example.newStudy;

import org.example.util.UUIDUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.File;

@Controller
@RequestMapping("/test")
public class Test {
    private static String parentPath = "C:/temp/1/";
    
    private static byte[] nowbytes;

    @RequestMapping("/test")
    public void getVideoStream(HttpServletResponse response) {
        FileUtils.getVideoOutStream(response,nowbytes);
    }

    public static void main(String[] args) throws Exception {
        FileUtils.buildDirectory(parentPath);
        int count = 6;
        while (count-- > 0) {
            //1、生成uuid文件夹
            String fileName = UUIDUtils.generateUUID(10);
            String dirPath = FileUtils.buildDirectory(parentPath + fileName);
            //2、获取回调方法中的bytes数组
            byte[] bytes = FileUtils.fileToBytes(new File("C:/video.mp4"));
            nowbytes=bytes;
            //3、生成mp4文件存储在uuid文件夹
            FileUtils.fileToBytes(bytes, dirPath, "Tom.mp4");
        }
    }

}



package org.example.newStudy;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Slf4j
public class FileUtils {
    private static final int CACHE_SIZE = 1024;

    /**
     * 根据路径和文件名后缀创建文件
     *
     * @param path
     * @param suffix
     * @return
     */
    public static String buildFile(String path, String suffix) {
        //获取文件路径以及文件名
        if (suffix == null || suffix.equals("")) {
        } else {
            path = path + suffix;
        }
        File file = new File(path);
        //若文件不存在
        if (!file.exists()) {
            try {
                //创建文件
                file.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return path;
    }

    /**
     * 根据路径创建文件夹
     *
     * @param path
     * @return
     */
    public static String buildDirectory(String path) {
        File file = new File(path);
        //若文件不存在
        if (!file.exists()) {
            try {
                //创建文件
                file.mkdirs();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return path;
    }

    /**
     * 视频流传给前端
     *
     * @param response
     * @param bytes
     */
    public static void getVideoOutStream(HttpServletResponse response, byte[] bytes) {
        OutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            outputStream.write(bytes);
            outputStream.flush();
        } catch (Exception e) {
            log.error("error", e);
            throw new RuntimeException(e);
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                log.error("error", e);
            }
        }
    }

    /**
     * 将Byte数组转换成文件
     *
     * @param bytes    byte数组
     * @param filePath 文件路径  如 D://test/ 最后“/”结尾
     * @param fileName 文件名
     */
    public static void fileToBytes(byte[] bytes, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            if (!filePath.endsWith("/")) {
                filePath += "/";
            }
            file = new File(filePath + fileName);
            if (!file.getParentFile().exists()) {
                //文件夹不存在 生成
                file.getParentFile().mkdirs();
            }
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static byte[] fileToBytes(File file) throws Exception {
        byte[] data = new byte[0];
        if (file.exists()) {
            FileInputStream in = new FileInputStream(file);
            ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
            byte[] cache = new byte[CACHE_SIZE];
            int nRead = 0;
            while ((nRead = in.read(cache)) != -1) {
                out.write(cache, 0, nRead);
                out.flush();
            }
            out.close();
            in.close();
            data = out.toByteArray();
        }
        return data;
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值