第一步:需要在阿里云服务器上安装Docker(在这里我就不详细说明如何在阿里云安装Docker)
第二步:拉取镜像并启动 docker run -d --restart=always --privileged=true --net=host --name=fastdfs -e IP=192.168.14.1 -e WEB_PORT=80 -v ${HOME}/fastdfs:/var/local/fdfs registry.cn-beijing.aliyuncs.com/tianzuo/fastdfs
其中-v ${HOME}/fastdfs:/var/local/fdfs是指:将${HOME}/fastdfs这个目录挂载到容器里的/var/local/fdfs这个目录里。所以上传的文件将被持久化到${HOME}/fastdfs/storage/data里,IP 后面是自己的服务器公网ip或者虚拟机ip,-e WEB_PORT=80 指定nginx端口
//进入容器
docker exec -it fastdfs /bin/bash
//创建文件
echo "Hello FastDFS!">index.html
//测试文件上传
fdfs_test /etc/fdfs/client.conf upload index.html
阿里云控制台开放端口号
第三步:新建SpingBoot项目引入fastdfs依赖
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>${fastdfs-client.version}</version>
</dependency>
创建application.yml文件
fdfs:
so-timeout: 2500
connect-timeout: 600
thumb-image:
100
height: 100
tracker-list: # tracker服务配置地址列表
- 自己服务器IP不带http:22122
upload:
base-url: http://自己服务器IP/
allow-types:
- image/jpeg
- image/png
- image/bmp
- image/gif
- video/mp4
第四步:创建测试类进行文件上传
@Component
@EnableConfigurationProperties(UploadProperties.class)
public class Upload {
private Log log= LogFactory.getLog(UploadService.class);
@Autowired
private FastFileStorageClient storageClient;
@Autowired
private UploadProperties prop;
public String uploadImage(MultipartFile file) {
// 校验文件类型
String contentType = file.getContentType();
if (!prop.getAllowTypes().contains(contentType)) {
throw new RuntimeException("文件类型不支持");
}
// 校验文件内容
try {
// 校验文件内容
InputStream inputStream = file.getInputStream() ;
if (inputStream == null ) {
throw new RuntimeException("上传文件有问题");
}
} catch (IOException e) {
log.error("校验文件内容失败....{}", e);
throw new RuntimeException("校验文件内容失败"+e.getMessage());
}
try {
// 上传到FastDFS
// 获取扩展名
String extension = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), extension, null);
return prop.getBaseUrl() + storePath.getFullPath();
} catch (IOException e) {
log.error("【文件上传】上传文件失败!....{}", e);
throw new RuntimeException("【文件上传】上传文件失败!"+e.getMessage());
}
}
@ConfigurationProperties(prefix = "upload")
@Data
public class UploadProperties {
private String baseUrl;
private List<String> allowTypes;
如果你的SpringBoot为2.0以上的版本在文件上传时可能会抛出如下异常:
the request was rejected because its size (35678390) exceeds the configured maximum (10485760)
此时添加一个配置类:
@Configuration
public class MultipartConfig {
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//文件最大10M,DataUnit提供5中类型B,KB,MB,GB,TB
factory.setMaxFileSize(DataSize.of(200, DataUnit.MEGABYTES));
// 设置总上传数据总大小10M
factory.setMaxRequestSize(DataSize.of(200, DataUnit.MEGABYTES));
return factory.createMultipartConfig();
}
最后创建Controller层
@RequestMapping("/doUpload")
public Map<String, Object> doUpload (MultipartFile mf) {
Map<String, Object> map = new HashMap<String,Object>() ;
String image = this.uploadService.uploadImage(mf);
map.put("path",image) ;
return map ;
}