Loading

SpringBoot:整合FastDFS

一.pom.xml

 二.application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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整合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Component
public class FastUtil {
 
    private final Logger logger = LoggerFactory.getLogger(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 "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

1
2
3
4
5
6
@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FdfsConfig {
 
}

 五.Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!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,可以直接访问

 

 

 

参考博客:

https://www.cnblogs.com/dalianpai/p/11838009.html

 

posted @   秋风飒飒吹  阅读(649)  评论(1编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示