欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

SpringBoot整合FastDFS方法过程详解

时间:2020-11-14 12:07:24|栏目:JAVA代码|点击:

一.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 #超时时间
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 #单次单个文件最大大小
max-request-size: 50MB #单次上传所有文件的总大小<br>  #注意,这里springboot默认配置的大小是1MB和10MB,可能不够用,具体参考MultipartProperties.java

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

@Component
public class FastUtil {
 
  private final Logger logger = LoggerFactory.getLogger(FastUtil.class);
 
  @Autowired
  private FastFileStorageClient fastFileStorageClient;
 
  /**
   * 文件上传
   * 最后返回fastDFS中的文件名称;
   *
   * @param bytes   文件字节
   * @param fileSize 文件大小
   * @param extension 文件扩展名
   * @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("获取文件错误");
      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>
<h1 th:inlines="text">文件上传</h1>
<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>

七.开始上传

最后在页面上返回一个URL,可以直接访问

上一篇:Java编程二项分布的递归和非递归实现代码实例

栏    目:JAVA代码

下一篇:Java局部内部类和匿名内部类定义与用法实例分析

本文标题:SpringBoot整合FastDFS方法过程详解

本文地址:http://www.codeinn.net/misctech/22494.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有