SpringBoot整合FastDFS方法過程詳解
一.pom.xml
<?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.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>com.github.tobato</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>
二.application.yml
#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默認(rèn)配置的大小是1MB和10MB,可能不夠用,具體參考MultipartProperties.java

三.FastUtil.java 前提先將Nginx和FastDFS整合
@Component
public class FastUtil {
private final Logger logger = LoggerFactory.getLogger(FastUtil.class);
@Autowired
private FastFileStorageClient fastFileStorageClient;
/**
* 文件上傳
* 最后返回fastDFS中的文件名稱;
*
* @param bytes 文件字節(jié)
* @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 "http://111.111.111.111/"+storePath.getFullPath();
}
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 = LoggerFactory.getLogger(FdfsController.class);
@PostMapping("/upload")
@ResponseBody
public String upload(MultipartFile file) throws Exception {
byte[] bytes = new byte[0];
try {
bytes = file.getBytes();
} catch (IOException e) {
logger.error("獲取文件錯誤");
e.printStackTrace();
}
//獲取源文件名稱
String originalFileName = file.getOriginalFilename();
//獲取文件后綴--.doc
String extension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
String fileName = file.getName();
//獲取文件大小
long fileSize = file.getSize();
System.out.println(originalFileName + "==" + fileName + "==" + fileSize + "==" + extension + "==" + bytes.length);
String string = fastDFSClientWrapper.uploadFile(bytes, fileSize, extension);
return string;
}
}
六.前端頁面 index.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> <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,可以直接訪問


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java應(yīng)用服務(wù)器之tomcat會話復(fù)制集群配置的示例詳解
這篇文章主要介紹了Java應(yīng)用服務(wù)器之tomcat會話復(fù)制集群配置的相關(guān)知識,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Java采用循環(huán)鏈表結(jié)構(gòu)求解約瑟夫問題
這篇文章主要介紹了Java采用循環(huán)鏈表結(jié)構(gòu)求解約瑟夫問題的解決方法,是很多Java面試環(huán)節(jié)都會遇到的經(jīng)典考題,這里詳細(xì)給出了約瑟夫問題的原理及Java解決方法,是非常經(jīng)典的應(yīng)用實例,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12
SpringBoot @Cacheable自定義KeyGenerator方式
這篇文章主要介紹了SpringBoot @Cacheable自定義KeyGenerator方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Java 實戰(zhàn)項目之在線點餐系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實現(xiàn)一個在線點餐系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11

