springboot +fastdfs 上傳文件到到雲伺服器

来源:https://www.cnblogs.com/mcfeng/archive/2020/01/17/12206409.html
-Advertisement-
Play Games

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結構如下:

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。

 

 





您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ``` var xmlstr = @" some_appid 1413192605 component_verify_ticket some_verify_ticket "; Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i ...
  • 已有站點:HTTP80 %systemroot%\system32\inetsrv\APPCMD ADD APP /SITE.NAME:"HTTP80" /path:/Redirect /physicalPath:"C:\Windows\System32\drivers\etc" /applicat ...
  • 設計一個簡單的登錄視窗,要求輸入用戶名:小金,密碼:123456時候點登錄能正確轉到另一個視窗。 1、建立窗體應用。 2、這裡創建一個login和一個NewForm的窗體。 3、在login的窗體拖拉2個label和2個textbox和1個linklabel的控制項。一個標簽名字為用戶名,一個標簽為密 ...
  • Microsoft Visual Studio 2010 的項目為件改為Microsoft Visual Studio 2015預設打開 2010 的Solution (.Sln) file 更改為 2015 的Solution (.Sln) file ...
  • 本文將介紹如何在.NET Core3環境下使用MVVM框架Prism的使用事件聚合器實現模塊間的通信 一.事件聚合器 在上一篇 ".NET Core 3 WPF MVVM框架 Prism系列之模塊化" 我們留下了一些問題,就是如何處理同模塊不同窗體之間的通信和不同模塊之間不同窗體的通信,Prism提 ...
  • 官方資料:Shell Functions (Bash Reference Manual) 簡介 正如我們在《Bash腳本編程學習筆記06:條件結構體》中最後所說的,我們應該把一些可能反覆執行的代碼塊整合起來,避免反覆編寫使得代碼過於臃腫。 函數正是為瞭解決這個問題而存在的。函數在定義時,可以將常用的 ...
  • 先上目錄 chapter 3 [apue] dup2的正確打開方式 chapter 10 [apue] 等待子進程的那些事兒 chapter 14 [apue] 使用文件記錄鎖無法實現父子進程交互執行同步 chapter 15 [apue] 多進程管道讀寫的一些疑問 [apue] 測試管道容量的一些 ...
  • 在部署環境的時候,有時候會引用到虛擬主機的概念,什麼是虛擬主機呢,博主之前一直把虛擬主機的概念沒搞清楚,導致在部署的時候,一直動不動就404 ,或者500,或者伺服器不通 所以,什麼是虛擬主機呢? 虛擬主機概念大致同虛擬機的概念。 如果你有兩個不同功能變數名稱的網站,但是你只有一臺伺服器,這時候怎麼辦?其實 ...
一周排行
    -Advertisement-
    Play Games
  • GoF之工廠模式 @目錄GoF之工廠模式每博一文案1. 簡單說明“23種設計模式”1.2 介紹工廠模式的三種形態1.3 簡單工廠模式(靜態工廠模式)1.3.1 簡單工廠模式的優缺點:1.4 工廠方法模式1.4.1 工廠方法模式的優缺點:1.5 抽象工廠模式1.6 抽象工廠模式的優缺點:2. 總結:3 ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 本章將和大家分享ES的數據同步方案和ES集群相關知識。廢話不多說,下麵我們直接進入主題。 一、ES數據同步 1、數據同步問題 Elasticsearch中的酒店數據來自於mysql資料庫,因此mysql數據發生改變時,Elasticsearch也必須跟著改變,這個就是Elasticsearch與my ...
  • 引言 在我們之前的文章中介紹過使用Bogus生成模擬測試數據,今天來講解一下功能更加強大自動生成測試數據的工具的庫"AutoFixture"。 什麼是AutoFixture? AutoFixture 是一個針對 .NET 的開源庫,旨在最大程度地減少單元測試中的“安排(Arrange)”階段,以提高 ...
  • 經過前面幾個部分學習,相信學過的同學已經能夠掌握 .NET Emit 這種中間語言,並能使得它來編寫一些應用,以提高程式的性能。隨著 IL 指令篇的結束,本系列也已經接近尾聲,在這接近結束的最後,會提供幾個可供直接使用的示例,以供大伙分析或使用在項目中。 ...
  • 當從不同來源導入Excel數據時,可能存在重覆的記錄。為了確保數據的準確性,通常需要刪除這些重覆的行。手動查找並刪除可能會非常耗費時間,而通過編程腳本則可以實現在短時間內處理大量數據。本文將提供一個使用C# 快速查找並刪除Excel重覆項的免費解決方案。 以下是實現步驟: 1. 首先安裝免費.NET ...
  • C++ 異常處理 C++ 異常處理機制允許程式在運行時處理錯誤或意外情況。它提供了捕獲和處理錯誤的一種結構化方式,使程式更加健壯和可靠。 異常處理的基本概念: 異常: 程式在運行時發生的錯誤或意外情況。 拋出異常: 使用 throw 關鍵字將異常傳遞給調用堆棧。 捕獲異常: 使用 try-catch ...
  • 優秀且經驗豐富的Java開發人員的特征之一是對API的廣泛瞭解,包括JDK和第三方庫。 我花了很多時間來學習API,尤其是在閱讀了Effective Java 3rd Edition之後 ,Joshua Bloch建議在Java 3rd Edition中使用現有的API進行開發,而不是為常見的東西編 ...
  • 框架 · 使用laravel框架,原因:tp的框架路由和orm沒有laravel好用 · 使用強制路由,方便介面多時,分多版本,分文件夾等操作 介面 · 介面開發註意欄位類型,欄位是int,查詢成功失敗都要返回int(對接java等強類型語言方便) · 查詢介面用GET、其他用POST 代碼 · 所 ...
  • 正文 下午找企業的人去鎮上做貸後。 車上聽同事跟那個司機對罵,火星子都快出來了。司機跟那同事更熟一些,連我在內一共就三個人,同事那一手指桑罵槐給我都聽愣了。司機也是老社會人了,馬上聽出來了,為那個無辜的企業經辦人辯護,實際上是為自己辯護。 “這個事情你不能怪企業。”“但他們總不能讓銀行的人全權負責, ...