大文件大视频时使用断点续传

该代码实现了一个大视频文件的上传过程,首先通过MD5检查文件是否存在,若不存在则进行分片。使用RandomAccessFile从指定位置读取文件,多线程并行上传每个分片。上传后,检查分片文件并进行合并,最后对比源文件和合并后文件的MD5值以验证一致性。
摘要由CSDN通过智能技术生成

当上传一个大的视频时 首先通过文件的MD5判断 文件是否存在 如果不存在 则判断 该文件 是否从未上传 还是有上传的 但是没有上传完   如果从未上传 那么我们可以对这个文件进行分片 然后进行上传  如果该分片已经上传 那么跳过 代码如下

主要使用RandomAccessFile 它可以从指定位置开始读取

public void sliencce(MultipartFile file) throws Exception {
      //首先创建 上传到临时文件地址  进行分片 上传到指定的位置
    long start = System.currentTimeMillis();
    String originalFilename = file.getOriginalFilename();
    File file1=createTOFie(file);
     String fileType=getFileType(file);
     Long length=file.getSize();
     AtomicInteger count= new AtomicInteger();
    long start1 = System.currentTimeMillis();
      int le= (int) (length/SINCE_LENGTH);
    CountDownLatch countDownLatch = new CountDownLatch(28);
    ExecutorService executorService = Executors.newFixedThreadPool(30);
    for (int i = 0; i < length; i+=SINCE_LENGTH) {
        int finalI1 = i;
        executorService.execute(()->{
            count.getAndIncrement();
            String outFile="/D:/Users/博客项目/file/"+count+"."+fileType;
            boolean upload = isUpload(outFile);
            if (upload){
                return;
            }
            int finalI = finalI1;

            RandomAccessFile r = null;
            try {
                r = new RandomAccessFile(file1,"r");
                r.seek(finalI);  //从i位置开始读取
                byte[] bytes=new byte[SINCE_LENGTH];

                int read = r.read(bytes);

                FileOutputStream fileOutputStream=new FileOutputStream(new File(outFile));
                fileOutputStream.write(bytes,0,read);
                fileOutputStream.close();
                r.close();
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                countDownLatch.countDown();
            }
        });

    }
    file1.delete();
}

public boolean isUpload(String file){
    File file1 = new File(file);
   return file1.exists();
}

//合并文件 
@Override
public void merge() throws Exception {
    //原始文件
    File file = new File("C:/Users/msi-pc/Videos/12月20日/12月20日.mp4");
    //分片文件
    File since=new File("/D:/Users/博客项目/file/");
    //合并文件
    File merge = new File("/D:/Users/博客项目/12月20日.mp4");

    RandomAccessFile rw = new RandomAccessFile(merge, "rw");
    rw.seek(0);
    byte[] bytes=new byte[SINCE_LENGTH];
    File[] list = since.listFiles();
    List<File> strings = Arrays.asList(list);
    Collections.sort(strings, new Comparator<File>() {
        @Override
        public int compare(File o1, File o2) {

            return Integer.parseInt(o1.getName().substring(0,o1.getName().indexOf(".")))-Integer.parseInt(o2.getName().substring(0,o2.getName().indexOf(".")));
        }
    });
    //合并文件
    for (File file1 : strings) {
        RandomAccessFile r = new RandomAccessFile(file1, "r");
        int len=0;

        while ( (len= r.read(bytes))!=-1){
            rw.write(bytes,0,len);
        }
        r.close();
        file1.delete();
    }
    rw.close();
    //判断 两个文件是否一样
    FileInputStream fileInputStream = new FileInputStream(file);
    FileInputStream fileInputStream1 = new FileInputStream(merge);
    String s = DigestUtils.md5Hex(fileInputStream);
    String s1 = DigestUtils.md5Hex(fileInputStream1);
    if (s.equalsIgnoreCase(s1)){
        System.out.println("一样");
    }else{
        System.out.println("不一样");
    }

}

获取文件的类型

private String getFileType(MultipartFile file) {
    if (file==null){
        return null;
    }

    String originalFilename = file.getOriginalFilename();
    int i = originalFilename.lastIndexOf(".");
    return originalFilename.substring(i + 1);

}

private File createTOFie(MultipartFile file) throws Exception {
    String originalFilename = file.getOriginalFilename();
    String[] split = originalFilename.split("\\.");
    File tempFile = File.createTempFile(split[0] ,split[1]);
    file.transferTo(tempFile);
    return tempFile ;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值