1拉取镜像(已经内置Nginx)
docker pull delron/fastdfs
2 构建Tracker # 22122 => Tracker默认端口
docker run --name=tracker-server --privileged=true -p 22122:22122 -v /var/fdfs/tracker:/var/fdfs -d delron/fastdfs tracker
3 构建Storage # 23000 => Storage默认端口 # 8888 => 内置Nginx默认端口 # TRACKER_SERVER => 执行Tracker的ip和端口 # --net=host => 避免因为Docker网络问题导致外网客户端无法上传文件,因此使用host网络模式
docker run --name=storage-server --privileged=true -p 23000:23000 -p 8888:8888 -v /var/fdfs/storage:/var/fdfs -e TRACKER_SERVER=192.168.32.10:22122 -e GROUP_NAME=group1 --net=host -d delron/fastdfs storage
注意要修改成自己的ip地址
4 查看容器
docker ps
5 需要开启端口 腾讯云直接开启
8888 23000 22122
82.157.173.221:8888/group1/M00/00/00/CgAYBmMyxbuAH_jjAAFDcPEL5NE630.png
1 依赖
com.github.tobato
fastdfs-client
1.26.6
2 配置文件
fdfs:
# 读取时间
so-timeout: 1500
# 连接超时时间
connect-timeout: 600
# 缩略图
thumb-image:
width: 150
height: 150
# Tracker服务
tracker-list:
- 82.157.173.221:22122
# 读取时间
fdfs.so-timeout=1500
# 连接超时时间
fdfs.connect-timeout: 600
# 缩略图
fdfs.thumb-image.width=150
fdfs.thumb-image.height=150
# Tracker服务
fdfs.tracker-list=82.157.173.221:22122
3 配置类
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
/**
* FastDFS Client配置
*
* @author CL
*
*/
@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FdfsConfig {
}
4 工具类
package com.xinzhi.utils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.github.tobato.fastdfs.domain.fdfs.MetaData;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
/**
* FastDFS客户端包装类
*
* @author CL
*
*/
@Component
public class FdfsClientWrapper {
@Autowired
private FastFileStorageClient fastFileStorageClient;
public String uploadFile(MultipartFile file) throws IOException {
if (file != null) {
byte[] bytes = file.getBytes();
long fileSize = file.getSize();
String originalFilename = file.getOriginalFilename();
String extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
return this.uploadFile(bytes, fileSize, extension);
}
return null;
}
/**
* 文件上传
*
* @param bytes 文件字节
* @param fileSize 文件大小
* @param extension 文件扩展名
* @return 返回文件路径(卷名和文件名)
*/
public String uploadFile(byte[] bytes, long fileSize, String extension) {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
// 元数据
Set metaDataSet = new HashSet();
metaDataSet.add(new MetaData("dateTime", LocalDateTime.now().toString()));
StorePath storePath = fastFileStorageClient.uploadFile(bais, fileSize, extension, metaDataSet);
return storePath.getFullPath();
}
/**
* 下载文件
*
* @param filePath 文件路径
* @return 文件字节
* @throws IOException
*/
public byte[] downloadFile(String filePath) throws IOException {
byte[] bytes = null;
if (StringUtils.isNotBlank(filePath)) {
String group = filePath.substring(0, filePath.indexOf("/"));
String path = filePath.substring(filePath.indexOf("/") + 1);
DownloadByteArray byteArray = new DownloadByteArray();
bytes = fastFileStorageClient.downloadFile(group, path, byteArray);
}
return bytes;
}
/**
* 删除文件
*
* @param filePath 文件路径
*/
public void deleteFile(String filePath) {
if (StringUtils.isNotBlank(filePath)) {
fastFileStorageClient.deleteFile(filePath);
}
}
}
5 contoller
package com.xinzhi.controller;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import com.xinzhi.utils.FdfsClientWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
/**
* 文件上传Controller
*
* @author CL
*
*/
@Controller
public class FileUploadController {
private static Logger log = LoggerFactory.getLogger(FileUploadController.class);
@Autowired
private FdfsClientWrapper fdfsClientWrapper;
/**
* 进入上传页面
*
* @return 路径
*/
@RequestMapping(value = "/")
public String form() {
return "form";
}
/**
* 上传文件
*
* @param file 文件
* @return 文件路径
*/
@RequestMapping(value = "upload")
@ResponseBody
public String uploadFile(MultipartFile file) {
String filePath = null;
try {
filePath = fdfsClientWrapper.uploadFile(file);
} catch (IOException e) {
log.error("上传文件异常:{}", e);
return "上传文件失败";
}
System.out.println(filePath);
return filePath;
}
/**
* 下载文件
*
* @param filePath 文件路径
* @return
*/
@RequestMapping(value = "download")
public void downloadFile(String filePath, HttpServletResponse response) {
ServletOutputStream outputStream = null;
try {
byte[] bytes = fdfsClientWrapper.downloadFile(filePath);
String fileName = "fdfs.jpg";
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.setCharacterEncoding("UTF-8");
if (bytes != null) {
outputStream = response.getOutputStream();
outputStream.write(bytes);
outputStream.flush();
}
} catch (IOException e) {
log.debug("下载文件输出流异常:{}", e);
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
log.debug("下载文件关闭流异常:{}", e);
}
}
}
/**
* 删除文件
*
* @param filePath 文件路径
* @return 删除结果
*/
@RequestMapping(value = "delete")
@ResponseBody
public String deleteFile(String filePath) {
fdfsClientWrapper.deleteFile(filePath);
return "删除成功";
}
}
6 前端
上传文件
7 点击上传
group1/M00/00/00/CgAYBmMyxbuAH_jjAAFDcPEL5NE630.png
8 访问
ip:8888/group1/M00/00/00/CgAYBmMyxbuAH_jjAAFDcPEL5NE630.png
9 如果出现文件大小限制在启动类下配置
@Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize(DataSize.ofGigabytes(1));
factory.setMaxRequestSize(DataSize.ofGigabytes(1));
return factory.createMultipartConfig();
}