简单实现压缩视频

 //处理认证照片/视频
        List<String> imgIds = Lists.newArrayList();
        String[] array = {".jpg", ".jepg", ".png", ".gif", ".bmp", ".tiff", ".tif", ".raw", ".svg", ".ico", ".psd", ".ai", ".eps"};
        List<String> list = Arrays.asList(array);
        if (auditImgs != null && auditImgs.length > 0) {
            //循环获取file数组中得文件
            for (int i = 0; i < auditImgs.length; i++) {

                MultipartFile file = auditImgs[i];
                MultipartFile filepath = null;
                String fileUrl = "";
                //压缩文件大小
                try {
                    //filepath = VideoCompression.compressVideoByMultipartFile(file);
                    //拿到文件url
                    fileUrl = SystemUtils.storageFile(IdGen.uuid(), file.getBytes(), getFileExtension(file.getOriginalFilename()));
                    //转file
                    File file1 = MultipartFileAndFileUtils.toFile(file);
                    //拿到文件名忽略大小写
                    String name = file1.getName().toLowerCase();
                    //拿到字符最后出现的位置
                    int indexs = name.lastIndexOf(".");
                    //指定下标截取(这里是截取文件名后缀)
                    String substring = name.substring(indexs);
                    //判断是否为图片,是图片就直接上传
                    if (list.contains(substring)) {
                        FacePhoto facePhoto = new FacePhoto();
                        facePhoto.setUrl(fileUrl);
                        facePhoto.setOriginalFilename(file.getOriginalFilename());
                        facePhoto.setExtName(getFileExtension(file.getOriginalFilename()));
                        facePhoto.preInsert();
                        facePhotoDao.insert(facePhoto);
                        imgIds.add(facePhoto.getId());
                    } else {
                        //视频压缩上传
                        StringBuffer stringBuffer = new StringBuffer(fileUrl);
                        StringBuffer aNew = stringBuffer.insert(fileUrl.lastIndexOf("."), "New");
                        File newFile = new File(String.valueOf(aNew));
                        compress(file1, newFile);

                        System.out.println("压缩新文件地址" + newFile);
                        FacePhoto facePhoto = new FacePhoto();
                        facePhoto.setUrl(fileUrl);
                        facePhoto.setOriginalFilename(file.getOriginalFilename());
                        facePhoto.setExtName(getFileExtension(file.getOriginalFilename()));
                        facePhoto.preInsert();
                        facePhotoDao.insert(facePhoto);
                        imgIds.add(facePhoto.getId());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

必需的方法

package com.thinkgem.jeesite.modules.silence.web.test.test02;

import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class MultipartFileAndFileUtils {

    /**
     * MultipartFile 转 File
     * @param file
     * @throws Exception
     */
    public static File toFile(MultipartFile file) throws Exception {
        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }

    /**
     *  File 转 MultipartFile
     * @param file
     * @throws Exception
     */
    public static MultipartFile toMultipartFile(File file) throws Exception {
        InputStream inputStream = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),"application/octet-stream", inputStream);
        return multipartFile;
    }

    //获取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除本地临时文件
     * @param file
     */
    public static void delteTempFile(File file) {
        if (file != null) {
            File del = new File(file.toURI());
            del.delete();
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值