[Ubuntu] 安装FastDFS

Ubuntu安装FastDFS的方法:

https://zhuanlan.zhihu.com/p/29133587

导入依赖包:

<dependency>
      <groupId>com.github.tobato</groupId>
      <artifactId>fastdfs-client</artifactId>
      <!-- 版本也可以定义在父工程的pom中 -->
      <version>1.26.1-RELEASE</version>
</dependency>

配置 application.yml:

fdfs:
  so-timeout: 2001
  connect-timeout: 601
  thumb-image:
    width: 60
    height: 60
  tracker-list:
    - 192.168.77.130:22122

配置 FastDFS 配置类:

@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastClientImporter {
}

在IDEA中进行测试:

@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("C:\\Users\\Administrator\\Desktop\\wht.jpg"); // 本机文件地址
        // 上传并保存图片,参数:1-上传的文件流 2-文件的大小 3-文件的后缀
        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("C:\\Users\\Administrator\\Desktop\\wht.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);
    }
}

猜你喜欢

转载自www.cnblogs.com/fydra/p/12913495.html
今日推荐