网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

SpringBoot怎么整合FastDFS

这篇文章主要为大家展示了SpringBoot怎么整合FastDFS,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

创新互联是一家专业提供东至企业网站建设,专注与网站制作、网站设计、HTML5、小程序制作等业务。10年已为东至众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

一.pom.xml

<?xml version="1.0" encoding="UTF-8"?>

  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    2.2.6.RELEASE
     
  
  com.wj
  fastdsf-boot
  0.0.1-SNAPSHOT
  fastdsf-boot
  Demo project for Spring Boot
 
  
    1.8
  
 
  
    
      org.springframework.boot
      spring-boot-starter
    
 
    
      org.springframework.boot
      spring-boot-starter-web
    
 
    
      org.springframework.boot
      spring-boot-starter-thymeleaf
    
 
 
 
    
      com.github.tobato
      fastdfs-client
      1.26.2
    
 
    
      org.springframework.boot
      spring-boot-starter-test
      test
      
        
          org.junit.vintage
          junit-vintage-engine
        
      
    
  
 
  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  
 

二.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 #单次上传所有文件的总大小
  #注意,这里springboot默认配置的大小是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   文件字节
   * @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




  
  Insert title here


文件上传

选择文件:

七.开始上传

SpringBoot怎么整合FastDFS

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

SpringBoot怎么整合FastDFS

SpringBoot怎么整合FastDFS

以上就是关于SpringBoot怎么整合FastDFS的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。


本文标题:SpringBoot怎么整合FastDFS
当前路径:http://bjjierui.cn/article/iheceg.html

其他资讯