JAVA大文件切片笨方法

大文件切片上传前端VUE有指定的方法去操作,而java内我始终没有找到一个方法,就手写了一个笨方法。

   //文件最大多少M执行分片上传
private static  int BIG_SIZE = 1024 * 1024 * 100;
    //每片最大多少M
private static  int ONE_SIZE = 1024 * 1024 * 5;
========================================方法========================================

MultipartFile file = files[i];   // files为接口接收到的MultipartFile[] files集合
int size = Integer.parseInt(String.valueOf(file.getSize()));  //获取文件的大小

if(size >= BIG_SIZE){    //如果文件大于多少走切片  现在定义100M

   double db = (double) size/ONE_SIZE;  //定义每一片的文件大小  我们定义为5M
   int chunk = (int) Math.ceil(db);     //转为为整数得出一共切多少片
   for(int j=0; j<chunk; j++){
       InputStream inputStream = file.getInputStream();
       ByteArrayOutputStream bos = new ByteArrayOutputStream(); //创建输出流对象
       byte[] b = new byte[1024*1024*5];   // 每片大小
       int len;
       int x = 0; //第几片

       while ((len = inputStream.read(b)) != -1) {
           if(x == j){                    
               bos.write(b, 0, len);
           }else if(x > j ){
               break;
           }
           x++;
       }
    byte[] bytesOver = bos.toByteArray();
    MultipartFile multipartFile = new MockMultipartFile(uploadFileName, bytesOver);  //读出来的文件流 可用来上传操作了
   }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值