欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

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

程序员文章站 2022-06-22 12:22:35
fastdfs在云服务器的搭建和配置:https://blog.csdn.net/qq_41592652/article/details/104006289 springboot结构如下: application.properties配置如下: 1 server.port=8080 2 #单个文件最 ......

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

springboot结构如下:

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

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 

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>

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 }

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 }

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 }

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 }

启动类配置:

 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。