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.g