JAVA大文件分片上传

JAVA大文件分片上传

一 、思路

    1. 将大文件拆分成多个小文件多次上传, 全部上传完成后, 将这些分片合并成原始文件
    1. 合并过程中需要按顺序合并, 所以需要 chunkIndex 定位顺序
    1. 需要知道什么时候合并, 所以需要 totalChunks 确定分片总数, 后端判断 上传数量(后端记录) == 分片总数
    1. fileHash作为唯一key来标识文件, 比如: MD5计算
    1. 合并完成后删除分片, 清空缓存
    1. 防止文件占用过多jvm内存, 应使用流的方式进行文件传输, 避免使用file.getBytes()类似的方法导致oom, 本示例使用FileChannel的方式进行文件传输

二、代码示例

java 复制代码
package org.example.controller;


import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/api/test")
public class TestController {

    private Map<String, AtomicInteger> CHUNK_TOTAL_MAP = new ConcurrentHashMap<>();

    /**
     * 大文件分片上传
     * 思路: 1. 将大文件拆分成多个小文件多次上传, 全部上传完成后, 将这些分片合并成原始文件
     *      2. 合并过程中需要按顺序合并, 所以需要 chunkIndex 定位顺序
     *      3. 需要知道什么时候合并, 所以需要 totalChunks 确定分片总数, 后端判断 上传数量(后端记录) == 分片总数
     *      4. fileHash作为唯一key来标识文件
     *      5. 合并完成后删除分片, 清空缓存
     * @param multipartFile 文件片段
     * @param chunkIndex 分片顺序编号, 从0开始
     * @param totalChunks 分片总数
     * @param fileHash 原文件hash值, 作为唯一key
     * @param fileName 合并后的文件名
     * @param endWith 合并后的文件后缀
     * @return
     */
    @PostMapping("/upload")
    public ResponseEntity<Object> upload(@RequestPart MultipartFile multipartFile,
                                         @RequestParam int chunkIndex,
                                         @RequestParam int totalChunks,
                                         @RequestParam String fileHash,
                                         @RequestParam String fileName,
                                         @RequestParam String endWith){

        //临时存储分片路径
        String chunkDir = "D:/file/upload/"+fileHash;
        String chunkPath = "/chunk_" + chunkIndex;

        //创建目录
        try {
            Files.createDirectories(Path.of(chunkDir));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //保存分片
        writeToChunk(multipartFile, chunkPath);

        //更新上传分片数量
        CHUNK_TOTAL_MAP.putIfAbsent(fileHash, new AtomicInteger(0));
        int uploadChunks = CHUNK_TOTAL_MAP.get(fileHash).incrementAndGet();

        //判断是否开始合并
        if(uploadChunks == totalChunks){
            mergeChunk(chunkDir, fileName, totalChunks, endWith);
            CHUNK_TOTAL_MAP.remove(fileHash); //清空缓存
        }

        return ResponseEntity.ok(CHUNK_TOTAL_MAP);
    }

    /**
     * 合并分片
     * @param chunkDir 文件保存路径
     * @param fileName 文件名
     * @param totalChunks 分片总数
     * @param endWith 合并后的文件后缀
     */
    private void mergeChunk(String chunkDir, String fileName, int totalChunks, String endWith) {
        String filePath = chunkDir + "/" +fileName + "." + endWith;
        try (FileChannel outChannel = (FileChannel) Files.newByteChannel(Path.of(filePath), StandardOpenOption.CREATE, StandardOpenOption.APPEND)){

            for (int i = 0; i < totalChunks; i++) {
                //读取分片
                String chunkPath = "/chunk_" + i;
                try(FileChannel inChannel = (FileChannel) Files.newByteChannel(Path.of(chunkPath), StandardOpenOption.READ)){
                    //合并分片到主文件
                    outChannel.transferFrom(inChannel,outChannel.size(), inChannel.size());
                    //删除分片
                    Files.deleteIfExists(Path.of(chunkPath));
                }catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    /**
     * 保存分片
     * @param multipartFile
     * @param chunkPath
     */
    private void writeToChunk(MultipartFile multipartFile, String chunkPath) {

        try (FileChannel outChannel = (FileChannel) Files.newByteChannel(Path.of(chunkPath), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
             InputStream inputStream = multipartFile.getInputStream();
             ReadableByteChannel inChannel =  Channels.newChannel(inputStream);){

            long chunkSize = multipartFile.getSize();
            outChannel.transferFrom(inChannel,0, chunkSize);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
相关推荐
(Charon)20 小时前
【C++ 面试高频:内存管理、RAII 和智能指针详解】
java·开发语言·word
凡人叶枫20 小时前
Effective C++ 条款39:明智而审慎地使用 private 继承
java·数据库·c++·嵌入式开发
轻刀快马20 小时前
跨越软硬件的共鸣(二):从 Cache 写策略看 Redis 与 DB 的一致性博弈
java·开发语言·redis·计算机组成原理
折哥的程序人生 · 物流技术专研20 小时前
Java 23 种设计模式:从踩坑到精通 | 装饰器模式 —— 比继承更灵活的扩展方式,你用过吗?
java·装饰器模式·java面试·结构型模式·java设计模式·javaio·从踩坑到精通
lili001220 小时前
2026 企业 AI 选型新范式:OpenRouter Fusion 证明多模型融合性价比远超单模型,企业该如何重构技术栈? - 微元算力(weytoken)
java·人工智能·python·重构·ai编程
shushangyun_20 小时前
汽车服务行业B2B平台+AI解决方案哪家专业:2026年最新测评
java·运维·网络·数据库·人工智能·汽车
A.说学逗唱的Coke20 小时前
【大模型专题】Spring AI Alibaba × Skill 整合实战:让 AI 真正“会干活
java·人工智能·spring
大黄说说21 小时前
深入理解 Go 协程 Goroutine:并发编程的核心精髓
java·数据库·python
许彰午21 小时前
38_Java设计模式之装饰器模式
java·设计模式·装饰器模式
折哥的程序人生 · 物流技术专研21 小时前
Java 23 种设计模式:从踩坑到精通 | 组合模式 —— 树形结构处理,部分与整体一视同仁
java·组合模式·java面试·springsecurity·结构型模式·java设计模式·从踩坑到精通