post文件fastdfs服务器,springboot +fastdfs 上传文件到到云服务器

本文介绍了如何在SpringBoot应用中集成FastDFS,包括配置文件、FdfsConfig和FdfsConfiguration类的使用,以及CommonFileUtil类实现文件上传功能。重点讲解了如何配置FastDFS服务器,上传单文件、流文件及生成缩略图,并演示了上传文件到云服务器的过程。
摘要由CSDN通过智能技术生成

c4468b3f4df77e96b0a416fa2a870fba.png

springboot +fastdfs 上传文件到到云服务器

fastdfs在云服务器的搭建和配置:https://blog.csdn.net/qq_41592652/article/details/104006289

springboot结构如下:

3a07b530fb95d60a2e8093e5509fa73f.png

application.properties配置如下:

f5a606c356b1f0443ee24a9487ebe2ea.gif

58c0e8b7083ff28ba153926d34588453.gif

1 server.port=8080

2 #单个文件最大尺寸(设置100)3 spring.servlet.multipart.max-file-size=100MB4 #一个请求文件的最大尺寸5 spring.servlet.multipart.max-request-size=100MB6 #设置一个文件上传的临时文件目录7 spring.servlet.multipart.location=/root/temp8 #读取inputsream阻塞时间9 fdfs.connect-timeout=600

10 fdfs.so-timeout=1500

11 #tracker地址12 fdfs.trackerList=106.12.120.191:22122

13 #缩略图配置14 fdfs.thumbImage.height=150

15 fdfs.thumbImage.width=150

16 spring.jmx.enabled=false

17 #通过nginx 访问地址18 fdfs.resHost=106.12.120.191

19 #storage对应的端口20 fdfs.storagePort=23000

21 #获取连接池最大数量22 fdfs.pool.max-total=200

application.properties

pom.xml配置如下:

f5a606c356b1f0443ee24a9487ebe2ea.gif

58c0e8b7083ff28ba153926d34588453.gif

1 <?xml version="1.0" encoding="UTF-8"?>

2

3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4 4.0.0

5

6 org.springframework.boot

7 spring-boot-starter-parent

8 2.2.3.BUILD-SNAPSHOT

9

10

11 com.whizen

12 file

13 0.0.1-SNAPSHOT

14 file

15 Demo project for Spring Boot

16 war

17

18

19

20 1.8

21

22

23

24

25 org.springframework.boot

26 spring-boot-starter-jdbc

27

28

29 org.springframework.boot

30 spring-boot-starter-web

31

32

33

34 org.springframework.boot

35 spring-boot-devtools

36 runtime

37 true

38

39

40 log4j

41 log4j

42 1.2.17

43

44

45 com.github.tobato

46 fastdfs-client

47 1.26.1-RELEASE

48

49

50 org.springframework.boot

51 spring-boot-starter-test

52 test

53

54

55 org.junit.vintage

56 junit-vintage-engine

57

58

59

60

61

62

63

64

65 org.springframework.boot

66 spring-boot-maven-plugin

67

68

69 root

70

71

pom.xml

FdfsConfig类如下:

f5a606c356b1f0443ee24a9487ebe2ea.gif

58c0e8b7083ff28ba153926d34588453.gif

1 packagecom.whizen.file.configure;2 importorg.springframework.beans.factory.annotation.Value;3 importorg.springframework.stereotype.Component;4

5 /**

6 * FdfsConfig主要用以连接fastdfs,FdfsConfiguration使配置生效7 */

8 @Component9 public classFdfsConfig {10 @Value("${fdfs.resHost}")11 privateString resHost;12

13 @Value("${fdfs.storagePort}")14 privateString storagePort;15

16 publicString getResHost() {17 returnresHost;18 }19

20 public voidsetResHost(String resHost) {21 this.resHost =resHost;22 }23

24 publicString getStoragePort() {25 returnstoragePort;26 }27

28 public voidsetStoragePort(String storagePort) {29 this.storagePort =storagePort;30 }31

32 }

FdfsConfig

FdfsConfiguration类如下:

f5a606c356b1f0443ee24a9487ebe2ea.gif

58c0e8b7083ff28ba153926d34588453.gif

1 packagecom.whizen.file.configure;2 importorg.springframework.context.annotation.Configuration;3 importorg.springframework.context.annotation.EnableMBeanExport;4 importorg.springframework.jmx.support.RegistrationPolicy;5

6 @Configuration7 @EnableMBeanExport(registration=RegistrationPolicy.IGNORE_EXISTING)8 public classFdfsConfiguration {9

10 }

FdfsConfiguration

ComonFileUtil类如下:

f5a606c356b1f0443ee24a9487ebe2ea.gif

58c0e8b7083ff28ba153926d34588453.gif

1 packagecom.whizen.file.configure;2

3 importcom.github.tobato.fastdfs.domain.MateData;4 importcom.github.tobato.fastdfs.domain.StorePath;5 importcom.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;6 importcom.github.tobato.fastdfs.service.FastFileStorageClient;7 importorg.apache.commons.io.FilenameUtils;8 importorg.apache.commons.lang3.StringUtils;9 importorg.slf4j.Logger;10 importorg.slf4j.LoggerFactory;11 importorg.springframework.beans.factory.annotation.Autowired;12 importorg.springframework.stereotype.Component;13 importorg.springframework.web.multipart.MultipartFile;14

15 import java.io.*;16 importjava.nio.charset.Charset;17 importjava.util.Set;18

19 @Component20 public classCommonFileUtil {21

22 private final Logger logger = LoggerFactory.getLogger(FdfsConfig.class);23

24 @Autowired25 privateFastFileStorageClient storageClient;26

27

28 /**

29 * MultipartFile类型的文件上传?30 *@paramfile31 *@return

32 *@throwsIOException33 */

34 public String uploadFile(MultipartFile file) throwsIOException {35 StorePath storePath =storageClient.uploadFile(file.getInputStream(), file.getSize(),36 FilenameUtils.getExtension(file.getOriginalFilename()), null);37 returngetResAccessUrl(storePath);38 }39

40 /**

41 * 普通的文件上传42 *43 *@paramfile44 *@return

45 *@throwsIOException46 */

47 public String uploadFile(File file) throwsIOException {48 FileInputStream inputStream = newFileInputStream(file);49 StorePath path =storageClient.uploadFile(inputStream, file.length(),50 FilenameUtils.getExtension(file.getName()), null);51 returngetResAccessUrl(path);52 }53

54 /**

55 * 带输入流形式的文件上传56 *57 *@paramis58 *@paramsize59 *@paramfileName60 *@return

61 */

62 public String uploadFileStream(InputStream is, longsize, String fileName) {63 StorePath path = storageClient.uploadFile(is, size, fileName, null);64 returngetResAccessUrl(path);65 }66

67 /**

68 * 将一段文本文件写到fastdfs的服务器上69 *70 *@paramcontent71 *@paramfileExtension72 *@return

73 */

74 publicString uploadFile(String content, String fileExtension) {75 byte[] buff = content.getBytes(Charset.forName("UTF-8"));76 ByteArrayInputStream stream = newByteArrayInputStream(buff);77 StorePath path = storageClient.uploadFile(stream, buff.length, fileExtension, null);78 returngetResAccessUrl(path);79 }80

81 /**

82 * 返回文件上传成功后的地址名称?83 *@paramstorePath84 *@return

85 */

86 privateString getResAccessUrl(StorePath storePath) {87 String fileUrl =storePath.getFullPath();88 returnfileUrl;89 }90

91 /**

92 * 删除文件93 *@paramfileUrl94 */

95 public voiddeleteFile(String fileUrl) {96 if(StringUtils.isEmpty(fileUrl)) {97 return;98 }99 try{100 StorePath storePath =StorePath.praseFromUrl(fileUrl);101 storageClient.deleteFile(storePath.getGroup(), storePath.getPath());102 } catch(FdfsUnsupportStorePathException e) {103 logger.warn(e.getMessage());104 }105 }106

107 public String upfileImage(InputStream is, long size, String fileExtName, SetmetaData) {108 StorePath path =storageClient.uploadImageAndCrtThumbImage(is, size, fileExtName, metaData);109 returngetResAccessUrl(path);110 }111

112 }

CommonFileUtil

fileControll控制类如下:

f5a606c356b1f0443ee24a9487ebe2ea.gif

58c0e8b7083ff28ba153926d34588453.gif

1 packagecom.whizen.file.controller;2

3 importcom.whizen.file.configure.CommonFileUtil;4 importorg.slf4j.Logger;5 importorg.slf4j.LoggerFactory;6 importorg.springframework.beans.factory.annotation.Autowired;7 importorg.springframework.stereotype.Controller;8 importorg.springframework.web.bind.annotation.CrossOrigin;9 importorg.springframework.web.bind.annotation.RequestMapping;10 importorg.springframework.web.bind.annotation.RequestParam;11 importorg.springframework.web.bind.annotation.ResponseBody;12 importorg.springframework.web.multipart.MultipartFile;13

14 importjava.io.IOException;15

16 @Controller17 public classfileControll {18 private final static Logger logger = LoggerFactory.getLogger(fileControll.class);19

20 @Autowired21 privateCommonFileUtil fileUtil;22 @CrossOrigin23 @ResponseBody24 @RequestMapping("/fileup")25 public String uoloadFileToFast(@RequestParam("file") MultipartFile file) throwsIOException {26

27 if(file.isEmpty()){28 System.out.println("文件不存在");29 }30 String path =fileUtil.uploadFile(file);31 System.out.println(path);32 return "success";33 }34 }

fileControll

启动类配置:

f5a606c356b1f0443ee24a9487ebe2ea.gif

58c0e8b7083ff28ba153926d34588453.gif

1 packagecom.whizen.file;2

3 importcom.github.tobato.fastdfs.FdfsClientConfig;4 importorg.springframework.boot.SpringApplication;5 importorg.springframework.boot.autoconfigure.SpringBootApplication;6 importorg.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;7 importorg.springframework.boot.builder.SpringApplicationBuilder;8 importorg.springframework.boot.web.servlet.support.SpringBootServletInitializer;9 importorg.springframework.context.annotation.Import;10

11 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class})12 @Import(FdfsClientConfig.class)13 public class FileApplication extendsSpringBootServletInitializer {14 @Override15 protectedSpringApplicationBuilder configure(SpringApplicationBuilder builder) {16 return builder.sources(FileApplication.class);17 }18

19 public static voidmain(String[] args) {20 SpringApplication.run(FileApplication.class, args);21 }22

23 }

启动类

然后发布到服务器:

ok。

原文链接:https://www.cnblogs.com/mcfeng/p/12206409.html

如有疑问请与原作者联系

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值