如何使用Spring Boot 2.0结合FastDFS进行高效文件分布式上传?

2026-06-04 01:137阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计812个文字,预计阅读时间需要4分钟。

如何使用Spring Boot 2.0结合FastDFS进行高效文件分布式上传?

本文简要介绍了如何使用Spring Boot 2.0通过FastDFS实现文件分布式中上传。以下是一个示例代码,介绍了非详细过程,适用于有一定基础的学习者或工作者。

1. 引入依赖

xml com.github.tobato fastdfs-client 1.26.4

2. 配置FastDFS

在`application.properties`中配置FastDFS相关参数:

propertiesfastdfs.server-list=192.168.1.10:22122;192.168.1.11:22122

3. 创建文件上传接口

java@RestController@RequestMapping(/file)public class FileController {

@Autowired private FastdfsClient fastdfsClient;

@PostMapping(/upload) public String upload(@RequestParam(file) MultipartFile file) { try { String fileExtName=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(.)); FastdfsFile fastdfsFile=fastdfsClient.upload(file.getInputStream(), file.getSize(), fileExtName); return fastdfsFile.getRemoteFileName(); } catch (IOException e) { e.printStackTrace(); return null; } }}

4. 使用示例

java// 上传文件String remoteFileName=fileController.upload(file);

// 下载文件InputStream inputStream=fastdfsClient.download(remoteFileName);

以上代码仅提供基本的上传和下载功能,实际项目中可能需要添加更多功能,如文件列表展示、文件删除等。希望这个简要介绍能对您有所帮助。

如何使用Spring Boot 2.0结合FastDFS进行高效文件分布式上传?

这篇文章主要介绍了springboot2.0如何通过fastdfs实现文件分布式上传,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1. 引入依赖

在父工程中,我们已经管理了依赖,版本为:

<fastDFS.client.version>1.26.7</fastDFS.client.version>

因此,这里我们直接在工程的pom.xml中引入坐标即可:

<dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> </dependency>

@Configuration @Import(FdfsClientConfig.class) // 解决jmx重复注册bean的问题 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) public class FastClientImporter { }

2. 在application.yml文件中编写FastDFS属性

fdfs: so-timeout: 1501 # 超时时间 connect-timeout: 601 # 连接超时时间 thumb-image: # 缩略图 width: 60 height: 60 tracker-list: # tracker地址:你的虚拟机服务器地址+端口(默认是22122) - 192.168.0.22:22122

3. 测试

package com.leyou.upload.test; import com.github.tobato.fastdfs.domain.fdfs.StorePath; import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig; import com.github.tobato.fastdfs.service.FastFileStorageClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; /** * @author john * @date 2019/12/6 - 15:09 */ @SpringBootTest @RunWith(SpringRunner.class) public class FastDFSTest { @Autowired private FastFileStorageClient storageClient; @Autowired private ThumbImageConfig thumbImageConfig; @Test public void testUpload() throws FileNotFoundException { // 要上传的文件 File file = new File("D:\\imooc\\project\\images\\1.jpg"); // 上传并保存图片,参数:1-上传的文件流 2-文件的大小 3-文件的后缀 4-可以不管他 StorePath storePath = this.storageClient.uploadFile( new FileInputStream(file), file.length(), "jpg", null); // 带分组的路径 System.out.println(storePath.getFullPath()); // 不带分组的路径 System.out.println(storePath.getPath()); } @Test public void testUploadAndCreateThumb() throws FileNotFoundException { File file = new File("D:\\imooc\\project\\images\\2.jpg"); // 上传并且生成缩略图 StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage( new FileInputStream(file), file.length(), "png", null); // 带分组的路径 System.out.println(storePath.getFullPath()); // 不带分组的路径 System.out.println(storePath.getPath()); // 获取缩略图路径 String path = thumbImageConfig.getThumbImagePath(storePath.getPath()); System.out.println(path); } }

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

本文共计812个文字,预计阅读时间需要4分钟。

如何使用Spring Boot 2.0结合FastDFS进行高效文件分布式上传?

本文简要介绍了如何使用Spring Boot 2.0通过FastDFS实现文件分布式中上传。以下是一个示例代码,介绍了非详细过程,适用于有一定基础的学习者或工作者。

1. 引入依赖

xml com.github.tobato fastdfs-client 1.26.4

2. 配置FastDFS

在`application.properties`中配置FastDFS相关参数:

propertiesfastdfs.server-list=192.168.1.10:22122;192.168.1.11:22122

3. 创建文件上传接口

java@RestController@RequestMapping(/file)public class FileController {

@Autowired private FastdfsClient fastdfsClient;

@PostMapping(/upload) public String upload(@RequestParam(file) MultipartFile file) { try { String fileExtName=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(.)); FastdfsFile fastdfsFile=fastdfsClient.upload(file.getInputStream(), file.getSize(), fileExtName); return fastdfsFile.getRemoteFileName(); } catch (IOException e) { e.printStackTrace(); return null; } }}

4. 使用示例

java// 上传文件String remoteFileName=fileController.upload(file);

// 下载文件InputStream inputStream=fastdfsClient.download(remoteFileName);

以上代码仅提供基本的上传和下载功能,实际项目中可能需要添加更多功能,如文件列表展示、文件删除等。希望这个简要介绍能对您有所帮助。

如何使用Spring Boot 2.0结合FastDFS进行高效文件分布式上传?

这篇文章主要介绍了springboot2.0如何通过fastdfs实现文件分布式上传,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1. 引入依赖

在父工程中,我们已经管理了依赖,版本为:

<fastDFS.client.version>1.26.7</fastDFS.client.version>

因此,这里我们直接在工程的pom.xml中引入坐标即可:

<dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> </dependency>

@Configuration @Import(FdfsClientConfig.class) // 解决jmx重复注册bean的问题 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) public class FastClientImporter { }

2. 在application.yml文件中编写FastDFS属性

fdfs: so-timeout: 1501 # 超时时间 connect-timeout: 601 # 连接超时时间 thumb-image: # 缩略图 width: 60 height: 60 tracker-list: # tracker地址:你的虚拟机服务器地址+端口(默认是22122) - 192.168.0.22:22122

3. 测试

package com.leyou.upload.test; import com.github.tobato.fastdfs.domain.fdfs.StorePath; import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig; import com.github.tobato.fastdfs.service.FastFileStorageClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; /** * @author john * @date 2019/12/6 - 15:09 */ @SpringBootTest @RunWith(SpringRunner.class) public class FastDFSTest { @Autowired private FastFileStorageClient storageClient; @Autowired private ThumbImageConfig thumbImageConfig; @Test public void testUpload() throws FileNotFoundException { // 要上传的文件 File file = new File("D:\\imooc\\project\\images\\1.jpg"); // 上传并保存图片,参数:1-上传的文件流 2-文件的大小 3-文件的后缀 4-可以不管他 StorePath storePath = this.storageClient.uploadFile( new FileInputStream(file), file.length(), "jpg", null); // 带分组的路径 System.out.println(storePath.getFullPath()); // 不带分组的路径 System.out.println(storePath.getPath()); } @Test public void testUploadAndCreateThumb() throws FileNotFoundException { File file = new File("D:\\imooc\\project\\images\\2.jpg"); // 上传并且生成缩略图 StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage( new FileInputStream(file), file.length(), "png", null); // 带分组的路径 System.out.println(storePath.getFullPath()); // 不带分组的路径 System.out.println(storePath.getPath()); // 获取缩略图路径 String path = thumbImageConfig.getThumbImagePath(storePath.getPath()); System.out.println(path); } }

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