FastDFS初体验

这次被带了节奏,底下人在没有经过我允许的情况下,悄悄用了FastDFS,极大地增加了给日本客户部署的难度。

底下人用了docker来安装FastDFS,过程完全照搬:https://blog.youkuaiyun.com/canyun9798/article/details/83795789,该步骤在CentOS中完全可行,只需要安装一下docker,安装过程可参考:https://docs.docker.com/install/linux/docker-ce/centos/

如果在Windows上安装,需要使用Docker Quickstart Terminal, 需要注意虚拟机的ip问题,参考:https://blog.youkuaiyun.com/qq_33591903/article/details/90751340

通过:

docker-machine ip default

将获得的ip配置到container tracker和storage中所有需要填写ip的地方。

在pom中添加依赖关系:

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

在Spring Boot的配置文件中添加:

fdfs:
  soTimeout: 1501
  connectTimeout: 601
  thumb-image:             #缩略图生成参数
    width: 150
    height: 150
  tracker-list: 192.168.99.100:8300 #多个 trackerServer中间以逗号分隔

写一个单测:

@RunWith(SpringRunner.class)
@SpringBootTest
public class FastDFSClientTest {

    @Autowired
    private FastDFSClient fastDFSClient;

    @Test
    public void testUploadFileFile() throws Exception {
        File file = new File("C:\\mypic\\data\\00\\00\\egg.jpg");
        String url =  fastDFSClient.uploadFile(file);
        System.out.println(url);
    }
} 

其中, FastDFSClient的相关代码如:

@Component
public class FastDFSClient {

    @Autowired
    private FastFileStorageClient storageClient;
    
    @Autowired
    private PictureMapper pictureMapper;
  

    public String uploadFile(File file) throws IOException {
        StorePath storePath = storageClient.uploadFile(new FileInputStream(file), FileUtils.sizeOf(file),
                FilenameUtils.getExtension(file.getName()), null);

        return storePath.getGroup() + "/" + storePath.getPath();
    }
}

上传成功后就可以通过浏览器输入返回的url查看文件了,如:http://192.168.99.100:8410/group1/M00/00/00/wKhjZF05HFKART2eAAA-2i9EGuw899.jpg

 

转载于:https://my.oschina.net/u/4042451/blog/3079232

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值