fastDfs实现端点续传,不用将文件分块

application-pro.yaml设置

fdfs:
  so-timeout: 1500
  connect-timeout: 600
  pool:
    jmx-enabled: false
  thumb-image:
    width: 100
    height: 100
    			你自己的远程和端口
  tracker-list: 192.168.163.130:22122

在springboot里面的启动类上面加这个
在这里插入图片描述

导入依赖

 <dependency>
      <groupId>com.github.tobato</groupId>
      <artifactId>fastdfs-client</artifactId>
      <version>1.26.1-RELEASE</version>
    </dependency>

代码

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.AppendFileStorageClient;
import com.sc.rdc.fastdfs.common.CommonResult;
import com.sc.rdc.fastdfs.service.Impl.BreakpointServiecImpl;
import com.sc.rdc.fastdfs.util.DisposeFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.sc.rdc.fastdfs.entity.Breakpoint;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Date;
import java.util.UUID;


@Autowired
    private AppendFileStorageClient appendFileStorageClient;
    
 	private final int BY = 1024 * 1024;
/**
     * 端点续传 , 没有将文件分块,直接把文件发到fastDfs
     *
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("uploadFile")
    public CommonResult uploadFile(@RequestParam(value = "uploadFile") MultipartFile file) throws Exception {
        //先是获取文件的md5,确保第二次上传文件是否为同一次文件
        String md5 = DigestUtils.md5DigestAsHex(file.getInputStream());
        // 创建文件上传ID
        String uploadId = UUID.randomUUID().toString();
//        从数据库取值,可以建你自己的数据库
        Breakpoint breakpoint = breakpointServiec.selectByMd5(md5);
        if (breakpoint == null) {
            breakpoint = new Breakpoint();
            breakpoint.setMd5(md5);
            breakpoint.setIda(uploadId);
        }
        // 获取文件信息
        String name = file.getOriginalFilename();
        String substring = name.substring(name.lastIndexOf(".") + 1);
        long fileSize = file.getSize();
        InputStream is = file.getInputStream();

        //已经上传的进度
        long len = BY;
        // 跳过已经上传
        is.skip(breakpoint.getChunkCurr());
        int i=0;
      	//非常重要的部分
        while (breakpoint.getChunkCurr() < fileSize) {
            len = Math.min(fileSize - breakpoint.getChunkCurr(), BY);
            if (breakpoint.getChunkCurr() != 0) {
                appendFileStorageClient.appendFile("group1", breakpoint.getUrl(), is, len);
            } else {
                StorePath storePath = appendFileStorageClient.uploadAppenderFile("group1", is, len, substring);
                breakpoint.setUrl(storePath.getPath());
            }
            // 修改上传进度,保存到数据库里面
            breakpoint.setChunkCurr(breakpoint.getChunkCurr() + len);
            System.out.println("上传进度 = "+((breakpoint.getChunkCurr()*1.0/fileSize)*100)+"%");
//           制造异常
//            i++;
//            if (i> 10) {
//                breakpointServiec.saveOrUpdate(breakpoint);
//                throw new RuntimeException("出问题了");
//            }
        }
        System.out.println("完成了");
        //保存信息到数据库里面,可以是你自己的数据库
        breakpoint.setName(name);
        breakpoint.setBegin(new Date());
        breakpoint.setSize((int) file.getSize());
        breakpointServiec.saveOrUpdate(breakpoint);
        //然后关掉流
        disposeFile.flushAndClose(is);
        return CommonResult.successData("成功");
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值