FastDFS入门小demo
-
创建项目(SpringBoot项目)
-
导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.1-RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
-
进行配置
server: port: 8082 spring: application: name: upload-service servlet: multipart: max-file-size: 5MB # 设置文件上传的大小 fdfs: so-timeout: 1501 connect-timeout: 601 thumb-image: # 缩略图 width: 60 height: 60 tracker-list: # tracker地址 - 192.168.0.155:22122
-
启动类
@SpringBootApplication @EnableDiscoveryClient public class LyUploadApplication { public static void main(String[] args) { SpringApplication.run(LyUploadApplication.class,args); } }
-
FastClient组件
@Configuration @Import(FdfsClientConfig.class) // 解决jmx重复注册bean的问题 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) public class FastClientImporter { }
-
单元测试
@RunWith(SpringRunner.class) @SpringBootTest(classes = LyUploadApplication.class) public class FdfsTest { @Autowired private FastFileStorageClient storageClient; @Autowired private ThumbImageConfig thumbImageConfig; @Test public void testUpload() throws FileNotFoundException { File file = new File("F:\\FastDFS\\tiger.jpg"); // 上传并且生成缩略图 StorePath storePath = this.storageClient.uploadFile( new FileInputStream(file), file.length(), "jpg", null); // 带分组的路径 System.out.println(storePath.getFullPath()); // 不带分组的路径 System.out.println(storePath.getPath()); } }
-
根据打印的路径访问:
-
小Demo收工,如有疑问,欢迎留言.