文件分块上传合并(带文件校验)

/**                                                                                                                                                        
 * 分块测试                                                                                                                                                    
 */                                                                                                                                                        
@Test                                                                                                                                                      
public void testChunk() throws IOException {                                                                                                               
                                                                                                                                                           
    //源文件                                                                                                                                                  
    File sourcefile=new File("H:\\XueCheng     PLUS\\学成在线资料\\学成在线项目—资料\\day06 断点续传 xxl-job\\资料\\xxl-job-2.3.1.zip");                                       
                                                                                                                                                           
    //分块文件存储路径                                                                                                                                             
    String chunkFilePath ="H:\\XueCheng     PLUS\\学成在线资料\\学成在线项目—资料\\day06 断点续传 xxl-job\\资料\\chunk\\";                                                     
                                                                                                                                                           
    //分块文件大小                                                                                                                                               
    int chunkSize =1024*1024*1; //1MB                                                                                                                      
                                                                                                                                                           
    //分块文件个数                                                                                                                                               
    int chunkNum = (int)Math.ceil(sourcefile.length() * 1.0 / chunkSize);                                                                                  
                                                                                                                                                           
    //使用流从源文件中读取数据,向分块文件中写数据(随机流 可以变成输入流 也能变成输出流)                                                                                                          
    RandomAccessFile raf_r = new RandomAccessFile(sourcefile,"r");                                                                                         
                                                                                                                                                           
    //缓冲区                                                                                                                                                  
    byte[] bytes =new byte[1024];                                                                                                                          
                                                                                                                                                           
                                                                                                                                                           
    //根据分块个数进行读写                                                                                                                                           
    for (int i = 0; i < chunkNum; i++) {                                                                                                                   
                                                                                                                                                           
        File chunkFile = new File(chunkFilePath+i);                                                                                                        
                                                                                                                                                           
        //分块文件的写入流                                                                                                                                         
        RandomAccessFile raf_rw = new RandomAccessFile(chunkFile,"rw");                                                                                    
                                                                                                                                                           
        int len =-1;                                                                                                                                       
        //循环读取raf_r流中的数据到缓冲区                                                                                                                               
        while ((len=raf_r.read(bytes))!=-1){                                                                                                               
             raf_rw.write(bytes,0,len); //读取缓冲区里的数据写到磁盘                                                                                                    
                                                                                                                                                           
            if (chunkFile.length()>=chunkSize){ //当这一块文件大小等于要分块的大小时停止读取                                                                                    
                break;                                                                                                                                     
            }                                                                                                                                              
                                                                                                                                                           
                                                                                                                                                           
        }                                                                                                                                                  
        //写源文件的流进行关闭                                                                                                                                       
        raf_rw.close();                                                                                                                                    
    }                                                                                                                                                      
    //读源文件的流进行关闭                                                                                                                                           
    raf_r.close();                                                                                                                                         
                                                                                                                                                           
                                                                                                                                                           
                                                                                                                                                           
}                                                                                                                                                          
/**                                                                                                                                                        
 * 将方块合并                                                                                                                                                   
 */                                                                                                                                                        
@Test                                                                                                                                                      
public void testMerge() throws IOException {                                                                                                               
    //块文件目录                                                                                                                                                
    File chunkFolder =new File("H:\\XueCheng     PLUS\\学成在线资料\\学成在线项目—资料\\day06 断点续传 xxl-job\\资料\\chunk\\");                                               
                                                                                                                                                           
    //源文件                                                                                                                                                  
    File sourcefile=new File("H:\\XueCheng     PLUS\\学成在线资料\\学成在线项目—资料\\day06 断点续传 xxl-job\\资料\\xxl-job-2.3.1.zip");                                       
                                                                                                                                                           
                                                                                                                                                           
    //合并后的文件                                                                                                                                               
    File mergeFile=new File("H:\\XueCheng     PLUS\\学成在线资料\\学成在线项目—资料\\day06 断点续传 xxl-job\\资料\\合并后的文件\\xxl-job-2.3.1.zip");                                
                                                                                                                                                           
    //取出所有分块文件(将块文件目录中的文件取出来)                                                                                                                              
    File[] files = chunkFolder.listFiles();                                                                                                                
    //取出来的可能是无序的                                                                                                                                           
    //将数组转为list                                                                                                                                            
    List<File> fileList = Arrays.asList(files);                                                                                                            
    //排序                                                                                                                                                   
    Collections.sort(fileList, new Comparator<File>() {                                                                                                    
        @Override                                                                                                                                          
        public int compare(File o1, File o2) {                                                                                                             
           return Integer.parseInt(o1.getName())-Integer.parseInt(o2.getName());                                                                           
        }                                                                                                                                                  
    });                                                                                                                                                    
    //向合并文件写的流                                                                                                                                             
    RandomAccessFile raf_rw =new RandomAccessFile(mergeFile,"rw");                                                                                         
                                                                                                                                                           
    //缓存区                                                                                                                                                  
    byte[] bytes=new byte[1024];                                                                                                                           
                                                                                                                                                           
    //遍历分块文件向合并的文件写                                                                                                                                        
    for (int i = 0; i < fileList.size(); i++) {                                                                                                            
        //读分分块的流                                                                                                                                           
        RandomAccessFile raf_r =new RandomAccessFile(fileList.get(i),"r");                                                                                 
                                                                                                                                                           
        int len=-1;                                                                                                                                        
        while ((len = raf_r.read(bytes))!=-1){                                                                                                             
            raf_rw.write(bytes,0,len);                                                                                                                     
        }                                                                                                                                                  
        //关闭输入流                                                                                                                                            
        raf_r.close();                                                                                                                                     
    }                                                                                                                                                      
    //关闭输出流                                                                                                                                                
    raf_rw.close();                                                                                                                                        
                                                                                                                                                           
    //合并完成后对合并的文件进行校验                                                                                                                                      
    FileInputStream fileInputStream = new FileInputStream(mergeFile);                                                                                      
    //源文件                                                                                                                                                  
    FileInputStream fileInputStream1=new FileInputStream(sourcefile);                                                                                      
    String mergeMd5Hash = DigestUtils.md5Hex(fileInputStream);                                                                                             
    String sourceMd5Hash= DigestUtils.md5Hex(fileInputStream1);                                                                                            
    if (mergeMd5Hash.equals(sourceMd5Hash)){                                                                                                               
        System.out.println("文件没有发生损坏");                                                                                                                    
    }else {                                                                                                                                                
        System.out.println("文件损坏");                                                                                                                        
    }                                                                                                                                                      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值