java工具类之大文件分片(切割)与合并

之前在潭州教育教学网站上看了一个视频关于java大文件的分片与合并

自己在练习的时候遇到一些坑,调试了好长时间

代码如下:

首先配置一个专门放参数的类

SplitFileParam

public class SplitFileParam {
    public static String file="C:\\Users\\pc\\Desktop\\photo/1.jpg"; //文件的路径
    public static String outfile="C:\\Users\\pc\\Desktop\\photo/out.jpg"; //文件的路径
    public static int count=10;   //将文件切割成多少份
}

SplitFile

package SplitFileUtil;

import jdk.nashorn.internal.runtime.logging.Logger;

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


public class SplitFile {

    public static void main(String[] args) {
        getSplitFile();
        merge(SplitFileParam.outfile,SplitFileParam.file,10);
    }

    /**
     * 文件分割方法
     */
    public static void getSplitFile() {
        String file = SplitFileParam.file; //文件的路径

        int count = SplitFileParam.count; //文件分割的份数
        RandomAccessFile raf = null;
        try {
            //获取目标文件 预分配文件所占的空间 在磁盘中创建一个指定大小的文件   r 是只读
            raf = new RandomAccessFile(new File(file), "r");
            long length = raf.length();//文件的总长度
            long maxSize = length / count;//文件切片后的长度
            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, i, begin, end);
            }
            if (length - offSet > 0) {
                getWrite(file, count-1, offSet, length);
            }

        } catch (FileNotFoundException e) {
            System.out.println("没有找到文件");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                raf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 指定文件每一份的边界,写入不同文件中
     * @param file 源文件
     * @param index 源文件的顺序标识
     * @param begin 开始指针的位置
     * @param end 结束指针的位置
     * @return long
     */
    public static long getWrite(String file,int index,long begin,long end){
        String a=file.split(".jpg")[0];
        long endPointer = 0L;
        try {
            //申明文件切割后的文件磁盘
            RandomAccessFile in = new RandomAccessFile(new File(file), "r");
            //定义一个可读,可写的文件并且后缀名为.tmp的二进制文件
            RandomAccessFile out = new RandomAccessFile(new File(a + "_" + index + ".tmp"), "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;
    }
    /**
     * 文件合并
     * @param file 指定合并文件
     * @param tempFile 分割前的文件名
     * @param tempCount 文件个数
     */
    public static void merge(String file,String tempFile,int tempCount) {
        String a=tempFile.split(".jpg")[0];
        RandomAccessFile raf = null;
        try {
            //申明随机读取文件RandomAccessFile
            raf = new RandomAccessFile(new File(file), "rw");
            //开始合并文件,对应切片的二进制文件
            for (int i = 0; i < tempCount; i++) {
                //读取切片文件
                RandomAccessFile reader = new RandomAccessFile(new File(a + "_" + i + ".tmp"), "r");
                byte[] b = new byte[1024];
                int n = 0;
                //先读后写
                while ((n = reader.read(b)) != -1) {//读
                    raf.write(b, 0, n);//写
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                raf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

上面是运行后完整的代码。

下面就说说我遇到坑,也怪我不细心


在分割文件的方法中,将两个条件弄反了,导致丢失一般的数据

上面是正确的截图

将两个条件反过来之后

结果是:


原因就是:

            while(in.getFilePointer() <= end && (n = in.read(b)) != -1){
            while( (n = in.read(b)) != -1&&in.getFilePointer() <= end){

上面是数据的指针是从零开始的

而下面的数据指针是从in读过之后的位置开始的,就是上面与下面的数据指针位置相差一个之前定义的byte数组长度个长度,这就是我测试是,数据丢失的原因

  • 6
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用以下工具类来实现Spring Boot与MinIO结合进行文件分片下载: ```java import io.minio.MinioClient; import io.minio.ObjectStat; import io.minio.errors.*; import io.minio.messages.Part; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.List; @Component public class MinioFileDownloader { @Autowired private MinioClient minioClient; public void downloadFile(String bucketName, String objectName, String filePath) throws IOException, NoSuchAlgorithmException, InvalidKeyException, InsufficientDataException, InternalException, InvalidResponseException, ErrorResponseException, XmlParserException, InvalidArgumentException { // 获取文件的元数据 ObjectStat objectStat = minioClient.statObject(bucketName, objectName); long fileSize = objectStat.length(); if (fileSize <= 0) { throw new IOException("File is empty"); } // 分片下载文件 int partSize = 5 * 1024 * 1024; // 分片大小为5MB long totalPartsCount = (long) Math.ceil((double) fileSize / partSize); for (int partNumber = 1; partNumber <= totalPartsCount; partNumber++) { long offset = (partNumber - 1) * partSize; long size = Math.min(partSize, fileSize - offset); InputStream inputStream = minioClient.getObject(bucketName, objectName, offset, size); try (BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) { byte[] buffer = new byte[(int) size]; bufferedInputStream.read(buffer, 0, (int) size); // 将分片写入文件 writeToFile(buffer, filePath); } } } private void writeToFile(byte[] data, String filePath) throws IOException { try (FileOutputStream outputStream = new FileOutputStream(filePath, true)) { outputStream.write(data); } } } ``` 你可以将以上代码添加到你的Spring Boot项目中,然后通过注入`MinioClient`来使用`MinioFileDownloader`类进行文件分片下载。请确保已经正确配置了MinIO客户端以及相应的依赖包。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值