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("成功");
}