溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot怎么整合FastDFS

發(fā)布時(shí)間:2020-07-28 15:01:37 來源:億速云 閱讀:169 作者:小豬 欄目:編程語言

這篇文章主要為大家展示了SpringBoot怎么整合FastDFS,內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來看看吧。

一.pom.xml

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.wj</groupId>
  <artifactId>fastdsf-boot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>fastdsf-boot</name>
  <description>Demo project for Spring Boot</description>
 
  <properties>
    <java.version>1.8</java.version>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
 
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
 
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
 
 
 
    <dependency>
      <groupId>com.github.tobato</groupId>
      <artifactId>fastdfs-client</artifactId>
      <version>1.26.2</version>
    </dependency>
 
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
 
</project>

二.application.yml

#fastdfs 配置
fdfs:
so-timeout: 150000
connect-timeout: 150000 #超時(shí)時(shí)間
thumb-image:
width: 150
height: 150
tracker-list:
- 111.111.111.111:22122 #ip:端口號
spring:
thymeleaf:
prefix: classpath:/templates/
servlet:
multipart:
max-file-size: 50MB #單次單個(gè)文件最大大小
max-request-size: 50MB #單次上傳所有文件的總大小<br>  #注意,這里springboot默認(rèn)配置的大小是1MB和10MB,可能不夠用,具體參考MultipartProperties.java

SpringBoot怎么整合FastDFS

三.FastUtil.java 前提先將Nginx和FastDFS整合

@Component
public class FastUtil {
 
  private final Logger logger = LoggerFactory.getLogger(FastUtil.class);
 
  @Autowired
  private FastFileStorageClient fastFileStorageClient;
 
  /**
   * 文件上傳
   * 最后返回fastDFS中的文件名稱;
   *
   * @param bytes   文件字節(jié)
   * @param fileSize 文件大小
   * @param extension 文件擴(kuò)展名
   * @return fastDfs路徑
   */
  public String uploadFile(byte[] bytes, long fileSize, String extension) {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    StorePath storePath = fastFileStorageClient.uploadFile(byteArrayInputStream, fileSize, extension, null);
    return "http://111.111.111.111/"+storePath.getFullPath();
  }
 
  public byte[] downloadFile(String group,String path) throws IOException {
    DownloadByteArray downloadByteArray = new DownloadByteArray();
    byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray);
    return bytes;
  }
 
}

四.配置類 FdfsConfig.java

@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FdfsConfig {
}

五.Controller

@RestController
public class FdfsController {
 
  @Autowired
  private FastUtil fastDFSClientWrapper;
 
  private final Logger logger = LoggerFactory.getLogger(FdfsController.class);
 
  @PostMapping("/upload")
  @ResponseBody
  public String upload(MultipartFile file) throws Exception {
    byte[] bytes = new byte[0];
    try {
      bytes = file.getBytes();
    } catch (IOException e) {
      logger.error("獲取文件錯(cuò)誤");
      e.printStackTrace();
    }
    //獲取源文件名稱
    String originalFileName = file.getOriginalFilename();
    //獲取文件后綴--.doc
    String extension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
    String fileName = file.getName();
    //獲取文件大小
    long fileSize = file.getSize();
    System.out.println(originalFileName + "==" + fileName + "==" + fileSize + "==" + extension + "==" + bytes.length);
    String string = fastDFSClientWrapper.uploadFile(bytes, fileSize, extension);
    return string;
  }
}

六.前端頁面 index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
   xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
  <meta charset="UTF-8" />
  <title>Insert title here</title>
</head>
<body>
<h2 th:inlines="text">文件上傳</h2>
<form action="upload" method="post" enctype="multipart/form-data">
  <p>選擇文件: <input type="file" name="file"/></p>
  <p><input type="submit" value="提交"/></p>
</form>
</body>
</html>

七.開始上傳

SpringBoot怎么整合FastDFS

最后在頁面上返回一個(gè)URL,可以直接訪問

SpringBoot怎么整合FastDFS

SpringBoot怎么整合FastDFS

以上就是關(guān)于SpringBoot怎么整合FastDFS的內(nèi)容,如果你們有學(xué)習(xí)到知識或者技能,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI