SpringBoot整合FastDFS⽅法过程详解⼀.l
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /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.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wj</groupId>
<artifactId>fastdsf-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fastdsf-boot</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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>bato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
⼆.l
#fastdfs 配置
fdfs:
so-timeout: 150000
connect-timeout: 150000 #超时时间
thumb-image:
width: 150
height: 150
tracker-list:
- 111.111.111.111:22122 #ip:端⼝号
spring:
thymeleaf:
prefix: classpath:/templates/
servlet:
multipart:
max-file-size: 50MB #单次单个⽂件最⼤⼤⼩
max-request-size: 50MB #单次上传所有⽂件的总⼤⼩<br>  #注意,这⾥springboot默认配置的⼤⼩是1MB和10MB,可能不够⽤,具体参考MultipartProperties.java
三.FastUtil.java 前提先将Nginx和FastDFS整合
@Component
public class FastUtil {
private final Logger logger = Logger(FastUtil.class);
@Autowired
private FastFileStorageClient fastFileStorageClient;
/**
* ⽂件上传
* 最后返回fastDFS中的⽂件名称;
*
* @param bytes  ⽂件字节
* @param fileSize ⽂件⼤⼩
* @param extension ⽂件扩展名
* @return fastDfs路径
*/
public String uploadFile(byte[] bytes, long fileSize, String extension) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
StorePath storePath = fastFileStorageClient.uploadFile(byteArrayInputStream, fileSize, extension, null);
return "111.111.111.111/"+FullPath();
}
public byte[] downloadFile(String group,String path) throws IOException {
DownloadByteArray downloadByteArray = new DownloadByteArray();
byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray);
return bytes;
}
}
四.配置类 FdfsConfig.java
@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FdfsConfig {
}
五.Controller
@RestController
public class FdfsController {
@Autowired
private FastUtil fastDFSClientWrapper;
private final Logger logger = Logger(FdfsController.class);
@PostMapping("/upload")
@ResponseBody
public String upload(MultipartFile file) throws Exception {
byte[] bytes = new byte[0];
try {
bytes = Bytes();
} catch (IOException e) {
<("获取⽂件错误");
e.printStackTrace();
}
//获取源⽂件名称
String originalFileName = OriginalFilename();
//获取⽂件后缀--.doc
String extension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
String fileName = Name();
//获取⽂件⼤⼩
long fileSize = Size();
System.out.println(originalFileName + "==" + fileName + "==" + fileSize + "==" + extension + "==" + bytes.length);    String string = fastDFSClientWrapper.uploadFile(bytes, fileSize, extension);
return string;
}
}
六.前端页⾯ index.html
<!DOCTYPE html>
<html xmlns="/1999/xhtml" xmlns:th=""
xmlns:sec="/thymeleaf-extras-springsecurity3">
thyme<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h1 th:inlines="text">⽂件上传</h1>
<form action="upload" method="post" enctype="multipart/form-data">
<p>选择⽂件: <input type="file" name="file"/></p>
<p><input type="submit" value="提交"/></p>
</form>
</body>
</html>
七.开始上传
最后在页⾯上返回⼀个URL,可以直接访问
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。