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

2020-01-17 09:30:42来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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

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

springboot结构如下:

application.properties配置如下:

 1 server.port=8080
 2 #单个文件最大尺寸(设置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类如下:

 1 package com.whizen.file.configure;
 2 import org.springframework.beans.factory.annotation.Value;
 3 import org.springframework.stereotype.Component;
 4 
 5 /**
 6  * FdfsConfig主要用以连接fastdfs,FdfsConfiguration使配置生效
 7  */
 8 @Component
 9 public class FdfsConfig {
10     @Value("${fdfs.resHost}")
11     private String resHost;
12 
13     @Value("${fdfs.storagePort}")
14     private String storagePort;
15 
16     public String getResHost() {
17         return resHost;
18     }
19 
20     public void setResHost(String resHost) {
21         this.resHost = resHost;
22     }
23 
24     public String getStoragePort() {
25         return storagePort;
26     }
27 
28     public void setStoragePort(String storagePort) {
29         this.storagePort = storagePort;
30     }
31 
32 }
FdfsConfig

FdfsConfiguration类如下:

 1 package com.whizen.file.configure;
 2 import org.springframework.context.annotation.Configuration;
 3 import org.springframework.context.annotation.EnableMBeanExport;
 4 import org.springframework.jmx.support.RegistrationPolicy;
 5 
 6 @Configuration
 7 @EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)
 8 public class FdfsConfiguration {
 9 
10 }
FdfsConfiguration

ComonFileUtil类如下:

  1 package com.whizen.file.configure;
  2 
  3 import com.github.tobato.fastdfs.domain.MateData;
  4 import com.github.tobato.fastdfs.domain.StorePath;
  5 import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
  6 import com.github.tobato.fastdfs.service.FastFileStorageClient;
  7 import org.apache.commons.io.FilenameUtils;
  8 import org.apache.commons.lang3.StringUtils;
  9 import org.slf4j.Logger;
 10 import org.slf4j.LoggerFactory;
 11 import org.springframework.beans.factory.annotation.Autowired;
 12 import org.springframework.stereotype.Component;
 13 import org.springframework.web.multipart.MultipartFile;
 14 
 15 import java.io.*;
 16 import java.nio.charset.Charset;
 17 import java.util.Set;
 18 
 19 @Component
 20 public class CommonFileUtil {
 21 
 22     private final Logger logger = LoggerFactory.getLogger(FdfsConfig.class);
 23 
 24     @Autowired
 25     private FastFileStorageClient storageClient;
 26 
 27 
 28     /**
 29      *    MultipartFile类型的文件上传?
 30      * @param file
 31      * @return
 32      * @throws IOException
 33      */
 34     public String uploadFile(MultipartFile file) throws IOException {
 35         StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
 36                 FilenameUtils.getExtension(file.getOriginalFilename()), null);
 37         return getResAccessUrl(storePath);
 38     }
 39 
 40     /**
 41      * 普通的文件上传
 42      *
 43      * @param file
 44      * @return
 45      * @throws IOException
 46      */
 47     public 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);
 51         return getResAccessUrl(path);
 52     }
 53 
 54     /**
 55      * 带输入流形式的文件上传
 56      *
 57      * @param is
 58      * @param size
 59      * @param fileName
 60      * @return
 61      */
 62     public String uploadFileStream(InputStream is, long size, String fileName) {
 63         StorePath path = storageClient.uploadFile(is, size, fileName, null);
 64         return getResAccessUrl(path);
 65     }
 66 
 67     /**
 68      * 将一段文本文件写到fastdfs的服务器上
 69      *
 70      * @param content
 71      * @param fileExtension
 72      * @return
 73      */
 74     public String uploadFile(String content, String fileExtension) {
 75         byte[] buff = content.getBytes(Charset.forName("UTF-8"));
 76         ByteArrayInputStream stream = new ByteArrayInputStream(buff);
 77         StorePath path = storageClient.uploadFile(stream, buff.length, fileExtension, null);
 78         return getResAccessUrl(path);
 79     }
 80 
 81     /**
 82      * 返回文件上传成功后的地址名称?
 83      * @param storePath
 84      * @return
 85      */
 86     private String getResAccessUrl(StorePath storePath) {
 87         String fileUrl = storePath.getFullPath();
 88         return fileUrl;
 89     }
 90 
 91     /**
 92      * 删除文件
 93      * @param fileUrl
 94      */
 95     public void deleteFile(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, Set<MateData> metaData) {
108         StorePath path = storageClient.uploadImageAndCrtThumbImage(is, size, fileExtName, metaData);
109         return getResAccessUrl(path);
110     }
111 
112 }
CommonFileUtil

fileControll控制类如下:

 1 package com.whizen.file.controller;
 2 
 3 import com.whizen.file.configure.CommonFileUtil;
 4 import org.slf4j.Logger;
 5 import org.slf4j.LoggerFactory;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.web.bind.annotation.CrossOrigin;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestParam;
11 import org.springframework.web.bind.annotation.ResponseBody;
12 import org.springframework.web.multipart.MultipartFile;
13 
14 import java.io.IOException;
15 
16 @Controller
17 public class fileControll {
18     private final static Logger logger = LoggerFactory.getLogger(fileControll.class);
19 
20     @Autowired
21     private CommonFileUtil fileUtil;
22     @CrossOrigin
23     @ResponseBody
24     @RequestMapping("/fileup")
25     public String uoloadFileToFast(@RequestParam("file") MultipartFile file) throws IOException {
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

启动类配置:

 1 package com.whizen.file;
 2 
 3 import com.github.tobato.fastdfs.FdfsClientConfig;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 7 import org.springframework.boot.builder.SpringApplicationBuilder;
 8 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
 9 import org.springframework.context.annotation.Import;
10 
11 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
12 @Import(FdfsClientConfig.class)
13 public class FileApplication extends SpringBootServletInitializer {
14     @Override
15     protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
16         return builder.sources(FileApplication.class);
17     }
18 
19     public static void main(String[] args) {
20         SpringApplication.run(FileApplication.class, args);
21     }
22 
23 }
启动类

然后发布到服务器:

ok。

 

 




			   

原文链接:https://www.cnblogs.com/mcfeng/p/12206409.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:[apue] 作为 daemon, 启动 Unix Domain Socket 侦听失败?

下一篇:Nginx 和Apache 中的虚拟主机的概念