VideoUtil.java 3.1 KB
package com.viontech.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.web.multipart.MultipartFile;

public class VideoUtil {
    public static SimpleDateFormat FORMAT_YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");

    public static String saveVideo(MultipartFile videofile, String videoPath, String videoName) throws IOException {
        if (videofile == null || videofile.isEmpty())
            return null;
        videoPath = videoPath + "/" + FORMAT_YYYY_MM_DD.format(new Date());
        File videoFile = null;
        String fileName = videofile.getOriginalFilename();
        String videoFilePath = videoPath + "/" + videoName;
        makerDir(videoPath);
        System.out.println("videoFilePath=" + videoFilePath);
        InputStream is = videofile.getInputStream();
        videoFile = new File(videoFilePath);
        videofile.transferTo(videoFile);
        return videoFilePath;
    }

    public static String save(InputStream videofile, String videoPath, String videoName) {
        if (videofile == null) {
            System.out.println("videofile == null");
            return null;
        }
        videoPath = videoPath + "/" + FORMAT_YYYY_MM_DD.format(new Date());
        String videoFilePath = videoPath + "/" + videoName;
        makerDir(videoPath);
        FileOutputStream fos = null;
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            fos = new FileOutputStream(videoFilePath);
            FileInputStream fis = (FileInputStream)videofile;
            inChannel = fis.getChannel();
            outChannel = fos.getChannel();
            inChannel.transferTo(0L, inChannel.size(), outChannel);
            inChannel.close();
            outChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return videoFilePath;
    }

    public static byte[] getBety(String filePath) throws IOException {
        File file = new File(filePath);
        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int)file.length()];
        inputFile.read(buffer);
        inputFile.close();
        return buffer;
    }

    public static InputStream getInputStream(String filePath) throws IOException {
        InputStream inputStream = new ByteArrayInputStream(getBety(filePath));
        return inputStream;
    }

    public static boolean makerDir(String filePath) {
        try {
            File dirFile = new File(filePath);
            if (!dirFile.exists()) {
                dirFile.setWritable(true, false);
                dirFile.mkdirs();
            }
            File fp = new File(filePath);
            dirFile = null;
            if (fp.exists()) {
                fp = null;
                return true;
            }
            fp = null;
            return false;
        } catch (Exception e) {
            return false;
        }
    }
}