Spring Boot 2.X 整合 FastDFS分布式文件系统 Demo.
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.csourcegroupId>
<artifactId>fastdfs-client-javaartifactId>
<version>1.27-SNAPSHOTversion>
dependency>
注:fastdfs-client-java依赖包需要自行打包,详情请参照:
fastdfs-client-java
config/fdfs_client.conf
:
connect_timeout=30
network_timeout=60
http.tracker_server_port=80
# 取决于自己的FastDFS服务器,和/etc/fdfs/storage.conf设置的tracker_server保持一致
tracker_server=xxx.xxx.xxx.xx:22122
application.properties
:
server.port=80
# 文件最大size
spring.servlet.multipart.max-file-size=2MB
# FastDFS服务器地址
app.base.url=http://xxx.xxx.xxx.xxx/
public class FastDFSClient {
private TrackerClient trackerClient;
private TrackerServer trackerServer;
private StorageServer storageServer;
private StorageClient1 storageClient;
public FastDFSClient(String conf) throws Exception {
if (conf.contains("classpath:")) {
conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
}
/**
* 上传文件方法
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
}
public String uploadFile(String fileName) throws Exception {
return uploadFile(fileName, null, null);
}
public String uploadFile(String fileName, String extName) throws Exception {
return uploadFile(fileName, extName, null);
}
/**
* 上传文件方法
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
}
public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
}
public String uploadFile(byte[] fileContent, String extName) throws Exception {
return uploadFile(fileContent, extName, null);
}
}
(新建上传页面index.html,成功后跳转页面success.html)
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上传Demotitle>
head>
<body>
<h2>单文件上传示例h2>
<div>
<form method="POST" enctype="multipart/form-data" action="/upload">
<p>
文件:<input type="file" name="file"/>
<input type="submit" value="上传"/>
p>
form>
div>
body>
html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
上传成功:
<img src="" th:src="${imgUrl}" width="50%">
body>
html>
@RestController
@Slf4j
public class UploadController {
/**
* fastDFS服务器地址
*/
@Value("${app.base.url}")
private String appUrl;
/**
* 访问上传页面
* @return
*/
@GetMapping("/index")
public Object index(){
return new ModelAndView("index");
}
/**
* 单文件上传
* @param file
* @return
* @throws Exception
*/
@PostMapping("/upload")
public Object upload(MultipartFile file) throws Exception {
// 文件名
String originalFilename = file.getOriginalFilename();
log.info("[文件类型] - [{}]", file.getContentType());
log.info("[文件名称] - [{}]", originalFilename);
log.info("[文件大小] - [{}]", file.getSize());
ModelAndView view = new ModelAndView("success");
FastDFSClient client = new FastDFSClient("config/fdfs_client.conf");
String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
log.info("[文件扩展名] = [{}]", extName);
String uploadFile = client.uploadFile(file.getBytes(), extName);
String imgUrl = appUrl + uploadFile;
view.addObject("imgUrl", imgUrl);
return view;
}
}
运行XXXApplication.main()
,访问http://localhost/index
上传图片,成功:
详细代码
GitHub:https://github.com/xudc0521/spring-boot-v2/tree/master/spring-boot-v2-fastdfs