首页>代码>spring boot+webuploader实现大文件分片上传实例>/uploadfile/src/main/java/com/main/uploadfile/controller/FileUploadAction.java
package com.main.uploadfile.controller;


import com.main.uploadfile.controller.request.MultipartFileParam;

import org.apache.commons.io.FileUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.RandomAccessFile;
import java.util.concurrent.atomic.AtomicLong;

@RestController
public class FileUploadAction {

    private final static Logger logger= LoggerFactory.getLogger(FileUploadAction.class);

    private static AtomicLong counter = new AtomicLong(0L);

    @Value("${web.upload-path}")
    private String uploadPath;

    @Value("${serverIP}")
    private String serviceIP;

    @RequestMapping("uploadfile")
    public Object uploadv2(MultipartFileParam param) throws Exception {


        String path =null;

        try {


            String prefix = "req_count:" + counter.incrementAndGet() + ":";
            System.out.println(prefix + "start !!!");
            //使用 工具类解析相关参数,工具类代码见下面

            System.out.println();
            System.out.println();
            System.out.println();

            logger.info(prefix + "chunks= " + param.getChunks());
            logger.info(prefix + "chunk= " + param.getChunk());
            logger.info(prefix + "chunkSize= " + param.getSize());

            //这个必须与前端设定的值一致
            long chunkSize = 1024 * 1024;

            String finalDirPath = uploadPath + "file/";


            String tempDirPath = finalDirPath + param.getId();

            String tempFileName = param.getName() + "_tmp";

            File confFile = new File(tempDirPath, param.getName() + ".conf");

            File tmpDir = new File(tempDirPath);

            File tmpFile = new File(tempDirPath, tempFileName);

            if (!tmpDir.exists()) {
                tmpDir.mkdirs();
            }

            RandomAccessFile accessTmpFile = new RandomAccessFile(tmpFile, "rw");

            RandomAccessFile accessConfFile = new RandomAccessFile(confFile, "rw");

            long offset = chunkSize * param.getChunk();
            //定位到该分片的偏移量
            accessTmpFile.seek(offset);
            //写入该分片数据
            accessTmpFile.write(param.getFile().getBytes());

            //把该分段标记为 true 表示完成
            System.out.println(prefix + "set part " + param.getChunk() + " complete");

            accessConfFile.setLength(param.getChunks());
            accessConfFile.seek(param.getChunk());
            accessConfFile.write(Byte.MAX_VALUE);

            //completeList 检查是否全部完成,如果数组里是否全部都是(全部分片都成功上传)
            byte[] completeList = FileUtils.readFileToByteArray(confFile);
            byte isComplete = Byte.MAX_VALUE;
            for (int i = 0; i < completeList.length && isComplete==Byte.MAX_VALUE; i++) {
                //与运算, 如果有部分没有完成则 isComplete 不是 Byte.MAX_VALUE
                isComplete = (byte)(isComplete & completeList[i]);

                System.out.println(prefix + "check part " + i + " complete?:" + completeList[i]);
            }

            if (isComplete == Byte.MAX_VALUE) {

                System.out.println(prefix + "upload complete !!" +
                        "---------------------------------------------------------");
                renameFile(tempDirPath +"/"+tempFileName,tempDirPath+"/"+param.getName());

                path ="/"+param.getId()+"/"+ param.getName();

            }


            accessTmpFile.close();
            accessConfFile.close();

            System.out.println(prefix + "end !!!");

        }catch (Exception e){
            e.printStackTrace();
        }


        if (path!=null){

            String returnPath ="http://"+serviceIP+":8089/file"+path;

            return returnPath;
        }

        return "还在上传中";
    }




    private void renameFile(String file, String toFile){
        File toBeRenamed = new File(file);
        //检查要重命名的文件是否存在,是否是文件
        if (!toBeRenamed.exists() || toBeRenamed.isDirectory()) {

            System.out.println("File does not exist: " + file);
            return;
        }

        File newFile = new File(toFile);

        //修改文件名
        if (toBeRenamed.renameTo(newFile)) {
            System.out.println("File has been renamed.");
        } else {
            System.out.println("Error renmaing file");
        }

    }
}
最近下载更多
淡心伤  LV11 2023年8月2日
lds85930  LV11 2023年3月2日
351024274  LV1 2022年11月11日
taoshen95  LV14 2022年4月13日
denliv_hui  LV13 2022年4月5日
雷迪斯俺的乡亲们  LV11 2022年3月9日
qwerasdQQ  LV13 2021年12月28日
cfdnfsa  LV1 2021年12月21日
dailiang  LV10 2021年11月22日
71376607  LV1 2021年11月8日
最近浏览更多
liuy0617 2月21日
暂无贡献等级
zgd_suny  LV7 2023年12月21日
hong5225 2023年10月18日
暂无贡献等级
27866849  LV4 2023年9月11日
淡心伤  LV11 2023年8月2日
shaoqi 2023年7月26日
暂无贡献等级
go_away15  LV7 2023年3月23日
暂无贡献等级
351024274  LV1 2022年11月11日
我们都是小怪兽  LV3 2022年9月6日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友