java文件拆分合并,基于Netty的文件断点续传,分片传输



import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.*;

public class Main {

    private static String path="E:/test/";
    private static String filename="个人简历.docx";
    private static String newfilename="个人简历_new.docx";
    //拆分大小,请保证拆分大小保证大于1 kB,大小自己控制
    private static final int count = 1;


    public static void main(String[] args) throws Exception {
        //保存拆分
        saveFile();
        //进行合并
        getMerge();
    }

    /**
     * 创建指定大小的文件
     *
     * @param size
     */
    public static void createBigFile(long size) {
        Path filePath = Paths.get("E:\\test\\个人简历_new.docx");

        try {
            if (!Files.exists(filePath)) {
                Files.createFile(filePath);
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        ByteBuffer buffer = ByteBuffer.allocate(1);
        try (FileChannel fileChannel = FileChannel.open(filePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
            fileChannel.position(size-1);
            fileChannel.write(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 拆分逻辑
     *
     * @throws Exception
     */
    public static void saveFile() throws Exception {
        RandomAccessFile randomAccessFile = new RandomAccessFile(path+filename, "r");
        FileChannel channel = randomAccessFile.getChannel();
        long size = channel.size();
        long l = size / 1024;
        if (l > count) {
            System.out.println(l);
            //计算前部整体
            int i;
            for (i = 0; i < l / count; i++) {
                byte[] files = getFiles(randomAccessFile, randomAccessFile.getFilePointer(), count*1024);
                FileOutputStream out = new FileOutputStream(path+filename+"." + (i + 1) + ".tmp");
                out.write(files);
                out.flush();
                out.close();
            }
            //计算尾巴
            long l1 = size - ((size / (count*1024)) * count*1024);
            System.out.println(l1);
            if (l1 > 0) {
                byte[] files = getFiles(randomAccessFile, randomAccessFile.getFilePointer(), (int) l1);
                FileOutputStream out = new FileOutputStream(path+filename+"." + (i + 1) + ".tmp");
                out.write(files);
                out.flush();
                out.close();
            }

            FileOutputStream out = new FileOutputStream(path+"拆分说明.tmp");
            //拆分文件名,拆分份数,拆分大小,最后一次拆分大小,被拆分文件总大小
            out.write((filename+"," + (i + 1) + "," + count + "," + l1 + "," + size).getBytes());
            out.flush();
            out.close();

            randomAccessFile.close();
        }

    }

    /**
     * 拆分文件
     *
     * @param file
     * @param seek
     * @param count
     * @return
     * @throws Exception
     */
    public static byte[] getFiles(RandomAccessFile file, long seek, int count) throws Exception {
        file.seek(seek);
        byte[] bytes = new byte[count];
        file.read(bytes);
        return bytes;
    }


    /**
     * 合并
     */
    public static void getMerge() throws Exception {
        FileInputStream inputStream = new FileInputStream(path+"拆分说明.tmp");
        FileChannel channel = inputStream.getChannel();
        byte[] bytes = new byte[(int) channel.size()];
        inputStream.read(bytes);
        String string = new String(bytes);
        String[] split = string.trim().split(",");
        //创建一个空的文件
        createBigFile(Long.parseLong(split[split.length - 1]));

        RandomAccessFile file = new RandomAccessFile(path+newfilename, "rw");

        for (int i = 0; i < Integer.parseInt(split[1]); i++) {
            System.out.println("拆分保存路径----》"+path + split[0] + "." + (i + 1) + ".tmp");
            FileInputStream fin = new FileInputStream(path + split[0] + "." + (i + 1) + ".tmp");
            byte[] byt;
            //合并尾部文件
            if (i == Integer.parseInt(split[1]) - 1) {
                byt = new byte[Integer.parseInt(split[3])];
            } else {
                byt = new byte[Integer.parseInt(split[2]) * 1024];
            }
            fin.read(byt);
            file.seek(file.getFilePointer());
            file.write(byt);

        }
        file.close();


    }


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值