栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合FastDFS方法过程详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合FastDFS方法过程详解

一.pom.xml



  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

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

@Component
public class FastUtil {
 
  private final Logger logger = LoggerFactory.getLogger(FastUtil.class);
 
  @Autowired
  private FastFileStorageClient fastFileStorageClient;
 
  
  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


文件上传

七.开始上传

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/134224.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号