1. 程式人生 > >spring集成:如何用傳統方式使用fastDFSClient

spring集成:如何用傳統方式使用fastDFSClient

row utf con 官方文檔 pub files ack conn path

最近一直在摸索如何使用帶有連接池的fastDFS客戶端連接,在mvnrepository網站上找到了一個客戶端,maven坐標如下:

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

可官方文檔上是使用spring-boot來集成的。

費了一些時間終於通過傳統xml形式,獲取到該客戶端中的連接客戶端對象。

配置內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
> <!--配置掃描包--> <context:component-scan base-package="com.github.tobato.fastdfs.service,com.github.tobato.fastdfs.domain"/> <!--配置連接管理器--> <bean id="trackerConnectionManager" class="com.github.tobato.fastdfs.conn.TrackerConnectionManager"> <constructor-arg
name="pool" ref="fdfsConnectionPool"> </constructor-arg> <!--配置fastDFS tracker 服務器 ip:port 地址--> <property name="trackerList"> <list> <value>192.168.24.39:22122</value> </list> </property> </bean> <!--配置連接池--> <bean id="fdfsConnectionPool" class="com.github.tobato.fastdfs.conn.FdfsConnectionPool"> <!--註入連接池配置--> <constructor-arg name="config" > <bean class="com.github.tobato.fastdfs.conn.ConnectionPoolConfig"/> </constructor-arg> <!--註入連接池工廠--> <constructor-arg name="factory" > <bean class="com.github.tobato.fastdfs.conn.PooledConnectionFactory"/> </constructor-arg> </bean> </beans>

具體使用說明:

1. 需要使用的client類在com.github.tobato.fastdfs.service包下,而service包依賴於一些 com.github.tobato.fastdfs.domain 包下的類。

  如 FastFileStorageClient、AppendFileStorageClient、GenerateStorageClient等

2. 使用代碼實例

import com.github.tobato.fastdfs.domain.StorePath;
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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.File;
import java.io.FileInputStream;

@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class FastDFSDemo extends AbstractJUnit4SpringContextTests {

    @Test
    public void uploadFile() throws Exception{
        File file = new File("/home/duhui/code/fastdfsdemo/src/test/resources/images/54af9bcdN78b67b5a.jpg");
        StorePath storePath = fastFileStorageClient.uploadFile(null, new FileInputStream(file), file.length(), "jpg");
    }

    @Autowired
    FastFileStorageClient fastFileStorageClient;

spring集成:如何用傳統方式使用fastDFSClient