springboot+fastdfs上传文件到到云服务器
fastdfs在云服务器的搭建和配置:https://blog.csdn.net/qq_41592652/article/details/104006289
springboot结构如下:
application.properties配置如下:
1 server.port=80802#单个文件最大尺寸(设置100)
3 spring.servlet.multipart.max-file-size=100MB
4#一个请求文件的最大尺寸
5 spring.servlet.multipart.max-request-size=100MB
6#设置一个文件上传的临时文件目录
7 spring.servlet.multipart.location=/root/temp
8#读取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配置如下:
1 <?xml version="1.0" encoding="UTF-8"?>2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <parent>
6 <groupId>org.springframework.boot</groupId>
7 <artifactId>spring-boot-starter-parent</artifactId>
8 <version>2.2.3.BUILD-SNAPSHOT</version>
9 <relativePath/> <!-- lookup parent from repository -->
10 </parent>
11 <groupId>com.whizen</groupId>
12 <artifactId>file</artifactId>
13 <version>0.0.1-SNAPSHOT</version>
14 <name>file</name>
15 <description>Demo project for Spring Boot</description>
16 <packaging>war</packaging>
17
18
19 <properties>
20 <java.version>1.8</java.version>
21 </properties>
22
23 <dependencies>
24 <dependency>
25 <groupId>org.springframework.boot</groupId>
26 <artifactId>spring-boot-starter-jdbc</artifactId>
27 </dependency>
28 <dependency>
29 <groupId>org.springframework.boot</groupId>
30 <artifactId>spring-boot-starter-web</artifactId>
31 </dependency>
32
33 <dependency>
34 <groupId>org.springframework.boot</groupId>
35 <artifactId>spring-boot-devtools</artifactId>
36 <scope>runtime</scope>
37 <optional>true</optional>
38 </dependency>
39 <dependency>
40 <groupId>log4j</groupId>
41 <artifactId>log4j</artifactId>
42 <version>1.2.17</version>
43 </dependency>
44 <dependency>
45 <groupId>com.github.tobato</groupId>
46 <artifactId>fastdfs-client</artifactId>
47 <version>1.26.1-RELEASE</version>
48 </dependency>
49 <dependency>
50 <groupId>org.springframework.boot</groupId>
51 <artifactId>spring-boot-starter-test</artifactId>
52 <scope>test</scope>
53 <exclusions>
54 <exclusion>
55 <groupId>org.junit.vintage</groupId>
56 <artifactId>junit-vintage-engine</artifactId>
57 </exclusion>
58 </exclusions>
59 </dependency>
60 </dependencies>
61
62 <build>
63 <plugins>
64 <plugin>
65 <groupId>org.springframework.boot</groupId>
66 <artifactId>spring-boot-maven-plugin</artifactId>
67 </plugin>
68 </plugins>
69 <finalName>root</finalName>
70 </build>
71 </project>
pom.xml
FdfsConfig类如下:
1package com.whizen.file.configure; 2import org.springframework.beans.factory.annotation.Value; 3import org.springframework.stereotype.Component; 45/**
6 * FdfsConfig主要用以连接fastdfs,FdfsConfiguration使配置生效
7*/
8@Component
9publicclass FdfsConfig {
10 @Value("${fdfs.resHost}")
11private String resHost;
12
13 @Value("${fdfs.storagePort}")
14private String storagePort;
15
16public String getResHost() {
17return resHost;
18 }
19
20publicvoid setResHost(String resHost) {
21this.resHost = resHost;
22 }
23
24public String getStoragePort() {
25return storagePort;
26 }
27
28publicvoid setStoragePort(String storagePort) {
29this.storagePort = storagePort;
30 }
31
32 }
FdfsConfig
FdfsConfiguration类如下:
1package com.whizen.file.configure; 2import org.springframework.context.annotation.Configuration; 3import org.springframework.context.annotation.EnableMBeanExport; 4import org.springframework.jmx.support.RegistrationPolicy; 56@Configuration
7 @EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)
8publicclass FdfsConfiguration {
9
10 }
FdfsConfiguration
ComonFileUtil类如下:
1package com.whizen.file.configure; 23import com.github.tobato.fastdfs.domain.MateData;
4import com.github.tobato.fastdfs.domain.StorePath;
5import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
6import com.github.tobato.fastdfs.service.FastFileStorageClient;
7import org.apache.commons.io.FilenameUtils;
8import org.apache.commons.lang3.StringUtils;
9import org.slf4j.Logger;
10import org.slf4j.LoggerFactory;
11import org.springframework.beans.factory.annotation.Autowired;
12import org.springframework.stereotype.Component;
13import org.springframework.web.multipart.MultipartFile;
14
15import java.io.*;
16import java.nio.charset.Charset;
17import java.util.Set;
18
19@Component
20publicclass CommonFileUtil {
21
22privatefinal Logger logger = LoggerFactory.getLogger(FdfsConfig.class);
23
24 @Autowired
25private FastFileStorageClient storageClient;
26
27
28/**
29 * MultipartFile类型的文件上传ַ
30 * @param file
31 * @return
32 * @throws IOException
33*/
34public String uploadFile(MultipartFile file) throws IOException {
35 StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
36 FilenameUtils.getExtension(file.getOriginalFilename()), null);
37return getResAccessUrl(storePath);
38 }
39
40/**
41 * 普通的文件上传
42 *
43 * @param file
44 * @return
45 * @throws IOException
46*/
47public String uploadFile(File file) throws IOException {
48 FileInputStream inputStream = new FileInputStream(file);
49 StorePath path = storageClient.uploadFile(inputStream, file.length(),
50 FilenameUtils.getExtension(file.getName()), null);
51return getResAccessUrl(path);
52 }
53
54/**
55 * 带输入流形式的文件上传
56 *
57 * @param is
58 * @param size
59 * @param fileName
60 * @return
61*/
62public String uploadFileStream(InputStream is, long size, String fileName) {
63 StorePath path = storageClient.uploadFile(is, size, fileName, null);
64return getResAccessUrl(path);
65 }
66
67/**
68 * 将一段文本文件写到fastdfs的服务器上
69 *
70 * @param content
71 * @param fileExtension
72 * @return
73*/
74public String uploadFile(String content, String fileExtension) {
75byte[] buff = content.getBytes(Charset.forName("UTF-8"));
76 ByteArrayInputStream stream = new ByteArrayInputStream(buff);
77 StorePath path = storageClient.uploadFile(stream, buff.length, fileExtension, null);
78return getResAccessUrl(path);
79 }
80
81/**
82 * 返回文件上传成功后的地址名称ַ
83 * @param storePath
84 * @return
85*/
86private String getResAccessUrl(StorePath storePath) {
87 String fileUrl = storePath.getFullPath();
88return fileUrl;
89 }
90
91/**
92 * 删除文件
93 * @param fileUrl
94*/
95publicvoid deleteFile(String fileUrl) {
96if (StringUtils.isEmpty(fileUrl)) {
97return;
98 }
99try {
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
107public String upfileImage(InputStream is, long size, String fileExtName, Set<MateData> metaData) {
108 StorePath path = storageClient.uploadImageAndCrtThumbImage(is, size, fileExtName, metaData);
109return getResAccessUrl(path);
110 }
111
112 }
CommonFileUtil
fileControll控制类如下:
1package com.whizen.file.controller; 23import com.whizen.file.configure.CommonFileUtil;
4import org.slf4j.Logger;
5import org.slf4j.LoggerFactory;
6import org.springframework.beans.factory.annotation.Autowired;
7import org.springframework.stereotype.Controller;
8import org.springframework.web.bind.annotation.CrossOrigin;
9import org.springframework.web.bind.annotation.RequestMapping;
10import org.springframework.web.bind.annotation.RequestParam;
11import org.springframework.web.bind.annotation.ResponseBody;
12import org.springframework.web.multipart.MultipartFile;
13
14import java.io.IOException;
15
16@Controller
17publicclass fileControll {
18privatefinalstatic Logger logger = LoggerFactory.getLogger(fileControll.class);
19
20 @Autowired
21private CommonFileUtil fileUtil;
22 @CrossOrigin
23 @ResponseBody
24 @RequestMapping("/fileup")
25public String uoloadFileToFast(@RequestParam("file") MultipartFile file) throws IOException {
26
27if(file.isEmpty()){
28 System.out.println("文件不存在");
29 }
30 String path = fileUtil.uploadFile(file);
31 System.out.println(path);
32return "success";
33 }
34 }
fileControll
启动类配置:
1package com.whizen.file; 23import com.github.tobato.fastdfs.FdfsClientConfig;
4import org.springframework.boot.SpringApplication;
5import org.springframework.boot.autoconfigure.SpringBootApplication;
6import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
7import org.springframework.boot.builder.SpringApplicationBuilder;
8import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
9import org.springframework.context.annotation.Import;
10
11 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
12 @Import(FdfsClientConfig.class)
13publicclass FileApplication extends SpringBootServletInitializer {
14 @Override
15protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
16return builder.sources(FileApplication.class);
17 }
18
19publicstaticvoid main(String[] args) {
20 SpringApplication.run(FileApplication.class, args);
21 }
22
23 }
启动类
然后发布到服务器:
ok。
以上是 springboot+fastdfs上传文件到到云服务器 的全部内容, 来源链接: utcz.com/z/512829.html