SpringBoot + FastDFS(只实现上传)
时间:2021-06-29 15:23:12
收藏:0
阅读:0
SpringBoot + FastDFS(只实现上传)
搭建好FastDFS服务器后,使用Springboot集成fastdfs-client实现文件上传。
1、搭建FastDFS服务
为了简洁配置FastDFS服务,使用Docker搭建环境。
https://www.cnblogs.com/mjoe/p/14946238.html
2、SpringBoot环境搭建
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mjoe</groupId>
<artifactId>FastDFS-SpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>FastDFS-SpringBoot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.tobato/fastdfs-client -->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
file.html 页面
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>fileUpload page</title>
</head>
<body>
<h1 th:inlines="text">文件上传</h1>
<form action="uploadFileToFast" method="post" enctype="multipart/form-data">
<p>
选择文件: <input type="file" name="fileName" />
</p>
<p>
<input type="submit" value="提交" />
</p>
</form>
</body>
</html>
success.html页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf+VUE+Element</title>
</head>
<body>
文件上传成功 !!!
</body>
</html>
application.yaml配置文件
fdfs:
connect-timeout: 600
so-timeout: 1500
tracker-list: 82.156.203.105:22122
thumb-image:
height: 150
width: 150
pool:
max-total: 200
resHost: 82.156.203.105
storagePort: 8001
spring:
thymeleaf:
cache: false
servlet:
multipart:
max-file-size: 10MB
jmx:
enabled: false
接下来需要初始化fastdfs的相关配置,以便初始化的时候做全局加载,使配置生效,这里创建了两个类,FdfsConfig,FdfsConfiguration,FdfsConfig主要用以连接fastdfs,FdfsConfiguration使配置生效:
package com.mjoe.fastdfsspringboot.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @ClassName : FdfsConfig
* @Description : 配置fdfs链接
* @Author : MJoeBoyae
* @Date: 2021-06-28 21:32
*/
@Component
@Data
public class FdfsConfig {
@Value("${fdfs.resHost}")
private String resHost;
@Value("${fdfs.storagePort}")
private String storagePort;
}
package com.mjoe.fastdfsspringboot.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.jmx.support.RegistrationPolicy;
/**
* @ClassName : FdfsConfiguration
* @Description : fdfs配置生效
* @Author : MJoeBoyae
* @Date: 2021-06-28 21:34
*/
@Configuration
@EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)
public class FdfsConfiguration {
}
下面是关于使用fastdfs的客户端工具封装的几个操作fastdfs服务器的工具类,这里直接贴上代码,具体的意思可以参考官方文档,工具类在这个文件里,CommonFileUtil
package com.mjoe.fastdfsspringboot.util;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
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 java.io.*;
import java.nio.charset.Charset;
/**
* @ClassName : CommonFileUtil
* @Description : fdfs file tools
* @Author : MJoeBoyae
* @Date: 2021-06-28 21:35
*/
@Component
@Slf4j
public class CommonFileUtil {
@Autowired
private FastFileStorageClient storageClient;
public String uploadFile(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()), null);
return getResAccessUrl(storePath);
}
/**
* 普通的文件上传
*
* @param file
* @return
* @throws IOException
*/
public String uploadFile(File file) throws IOException {
FileInputStream inputStream = new FileInputStream(file);
StorePath path = storageClient.uploadFile(inputStream, file.length(),
FilenameUtils.getExtension(file.getName()), null);
return getResAccessUrl(path);
}
/**
* 带输入流形式的文件上传
*
* @param is
* @param size
* @param fileName
* @return
*/
public String uploadFileStream(InputStream is, long size, String fileName) {
StorePath path = storageClient.uploadFile(is, size, fileName, null);
return getResAccessUrl(path);
}
/**
* 将一段文本文件写到fastdfs的服务器上
*
* @param content
* @param fileExtension
* @return
*/
public String uploadFile(String content, String fileExtension) {
byte[] buff = content.getBytes(Charset.forName("UTF-8"));
ByteArrayInputStream stream = new ByteArrayInputStream(buff);
StorePath path = storageClient.uploadFile(stream, buff.length, fileExtension, null);
return getResAccessUrl(path);
}
/**
* 返回文件上传成功后的地址名称?
* @param storePath
* @return
*/
private String getResAccessUrl(StorePath storePath) {
String fileUrl = storePath.getFullPath();
return fileUrl;
}
/**
* 删除文件
* @param fileUrl
*/
public void deleteFile(String fileUrl) {
if (StringUtils.isEmpty(fileUrl)) {
return;
}
try {
StorePath storePath = StorePath.parseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (FdfsUnsupportStorePathException e) {
log.warn(e.getMessage());
}
}
// public String upfileImage(InputStream is, long size, String fileExtName, Set<MateData> metaData) {
// StorePath path = storageClient.uploadImageAndCrtThumbImage(is, size, fileExtName, metaData);
// return getResAccessUrl(path);
// }
}
接下来写一个控制器,FileController,用以操作文件上传,这里模拟单文件上传,如果上传成功,fastdfs会返回给客户端一个地址,这个地址就是存放上传的文件的具体位置,下面看测试代码:
package com.mjoe.fastdfsspringboot.controller;
import com.mjoe.fastdfsspringboot.util.CommonFileUtil;
import lombok.extern.slf4j.Slf4j;
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.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/**
* @ClassName : FileController
* @Description : file
* @Author : MJoeBoyae
* @Date: 2021-06-28 21:41
*/
@Controller
@Slf4j
public class FileController {
@Autowired
private CommonFileUtil fileUtil;
@RequestMapping("/goIndex")
public String goIndex(){
log.info("进入主页面");
return "/file";
}
//使用fastdfs进行文件上传(这种写法比较简单)
@RequestMapping("/uploadFileToFast")
public String uoloadFileToFast(@RequestParam("fileName") MultipartFile file) throws IOException {
if(file.isEmpty()){
log.info("文件不存在");
}
String path = fileUtil.uploadFile(file);
System.out.println(path);
return "success";
}
}
启动成功,测试
评论(0)