Java给文件分片和合并

编写工具类,给文件分片

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;


public class SplitFileUtil {
    /**
     * 文件分割方法
     * file 文件
     * count 分割的数量
     * targetPath 存放路径
     * fileNumber 存放路径下生成文件夹用于存储分片文件
     */
    public static void getSplitFile(File file,int count,String targetPath,String fileNumber) {
        RandomAccessFile raf = null;
        try {
            //获取目标文件 预分配文件所占的空间 在磁盘中创建一个指定大小的文件   r 是只读
            raf = new RandomAccessFile(file, "r");
            long length = raf.length();//文件的总长度
            long maxSize = 1024*1024;//文件切片后的长度
            long offSet = 0L;//初始化偏移量
            for (int i = 0; i < count - 1; i++) { //最后一片单独处理
                long begin = offSet;
                long end = (i + 1) * maxSize;
//                offSet = writeFile(file, begin, end, i);
                offSet = getWrite(file.getPath(), i, begin, end,targetPath,fileNumber);
            }
            if (length - offSet > 0) {
                getWrite(file.getAbsolutePath(), count-1, offSet, length,targetPath,fileNumber);
            }

        } catch (FileNotFoundException e) {
            System.out.println("没有找到文件");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                raf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 指定文件每一份的边界,写入不同文件中
     * @param filePath 源文件
     * @param index 源文件的顺序标识
     * @param begin 开始指针的位置
     * @param end 结束指针的位置
     * @return long
     */
    public static long getWrite(String filePath,int index,long begin,long end,String targetPath,String fileNumber){
        long endPointer = 0L;
        try {
            //申明文件切割后的文件磁盘
            RandomAccessFile in = new RandomAccessFile(new File(filePath), "r");
            //定义一个可读,可写的文件并且后缀名为.tmp的二进制文件
            RandomAccessFile out = new RandomAccessFile(new File(targetPath +fileNumber + "_" + index + ".seg"), "rw");
            //申明具体每一文件的字节数组
            byte[] b = new byte[1024];
            int n = 0;
            //从指定位置读取文件字节流
            in.seek(begin);
            //判断文件流读取的边界
            while(in.getFilePointer() <= end && (n = in.read(b)) != -1){
                //从指定每一份文件的范围,写入不同的文件
                out.write(b, 0, n);
            }
            //定义当前读取文件的指针
            endPointer = in.getFilePointer();
            //关闭输入流
            in.close();
            //关闭输出流
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return endPointer;
    }

}

分片调用:传输好对应参数即可
例:

 File encryptFile = FileUtil.newFile(filePath);
 int count = (int) (encryptFile.length()/(1024*1024)) + 1; //文件分割的份数
 SplitFileUtil.getSplitFile(encryptFile,count,"路径"+"/",fileNumber);

文件合并

public void combineFileSegment(){
//playFilePath 合并生成的文件地址,带文件名称后缀
 File encryptFile = new File(playFilePath);
        if (!encryptFile.exists()) {
            FileUtil.touch(encryptFile);
            //合并文件部分,segmentFiles是分片文件的集合
            File[] segmentFiles = new File(BfileString.appendString(segmentDir, "/success/")).listFiles();
            //totalSegment 是分片文件的数量
            Integer totalSegment = 10;
            if (null != segmentFiles && (segmentFiles.length == totalSegment)) {
                FileChannel outChannel = new FileOutputStream(encryptFile).getChannel();
                LOGGER.info("开始合并文件");
                for (int i = 0; i < totalSegment; i++) {
                    FileChannel fc = new 
                    //依次把分片文件读取到,分片的时候每个分片的文件名只有最后一位不一样,例如: 分片1  分片2
                    FileInputStream("分片文件地址" + "/" "文件分片名称一致的部分"+i)).getChannel();
                    ByteBuffer bb = ByteBuffer.allocate(1024 * 1024);
                    while (fc.read(bb) != -1) {
                    	//写入缓冲区
                        bb.flip();
                        outChannel.write(bb);
                        bb.clear();
                    }
                    fc.close();
                }
                outChannel.close();
                LOGGER.info("合并文件完成");
            }
        }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值