fdfs往服务器上传文件超时,FastDFS 实现大文件分片上传

本文介绍了如何使用FastDFS实现大文件的分片上传,通过Java代码展示了分片上传的控制器和业务接口实现,利用Redis进行锁管理和状态跟踪,确保文件上传的正确性和并发安全性。同时,文章还提到了前端上传限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、引入fastdfs

com.github.tobato

fastdfs-client

1.25.2-RELEASE

cn.hutool

hutool-all

4.0.12

2、编写控制层Controller

@GetMapping("/check_before_upload")

@ApiOperation("分片上传前的检测")

public RespMsgBean checkBeforeUpload(@RequestParam("userId") Long userId, @RequestParam("fileMd5") String fileMd5) {

return fileService.checkFile(userId, fileMd5);

}

@PostMapping("/upload_big_file_chunk")

@ApiOperation("分片上传大文件")

public RespMsgBean uploadBigFileChunk(@RequestParam("file") @ApiParam(value="文件",required=true) MultipartFile file,

@RequestParam("userId") @ApiParam(value="用户id",required=true) Long userId,

@RequestParam("fileMd5") @ApiParam(value="文件MD5值",required=true) String fileMd5,

@RequestParam("fileName") @ApiParam(value="文件名称",required=true) String fileName,

@RequestParam("totalChunks") @ApiParam(value="总块数",required=true) Integer totalChunks,

@RequestParam("chunkNumber") @ApiParam(value="当前块数",required=true) Integer chunkNumber,

@RequestParam("currentChunkSize") @ApiParam(value="当前块的大小",required=true) Integer currentChunkSize,

@RequestParam("bizId") @ApiParam(value="业务Id",required=true)String bizId,

@RequestParam("bizCode") @ApiParam(value="业务编码",required=true)String bizCode) {

return fileService.uploadBigFileChunk(file, userId, fileMd5, fileName, totalChunks, chunkNumber, currentChunkSize, bizId, bizCode);

}

3、编写业务接口以及实现类

package com.xxxx.cloud.platfrom.common.file.service.impl;

import cn.hutool.core.convert.Convert;

import cn.hutool.core.io.FileUtil;

import cn.hutool.core.util.StrUtil;

import com.alibaba.fastjson.JSONObject;

import com.xxxx.cloud.platfrom.common.pojo.protocol.RespMsgBean;

import com.github.tobato.fastdfs.domain.StorePath;

import com.github.tobato.fastdfs.service.AppendFileStorageClient;

import com.google.gson.Gson;

import org.apache.commons.lang.StringUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Service;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import javax.imageio.ImageIO;

import javax.servlet.http.HttpServletRequest;

import java.awt.image.BufferedImage;

import java.io.*;

import java.net.URLDecoder;

import java.util.ArrayList;

import java.util.List;

import java.util.Objects;

import java.util.Set;

import java.util.stream.Collectors;

/**

* 〈一句话功能简述〉

* 〈文件接口实现〉

* @author xxxx

* @create 2019/7/1

* @since 1.0.0

*/

@Service

public class FileServiceImpl implements FileService {

private Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);

@Override

public RespMsgBean checkFile(Long us

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值