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

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

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

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

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

配置內容以下: spring

<?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 包下的類。app

  如 FastFileStorageClient、AppendFileStorageClient、GenerateStorageClient等dom

2. 使用代碼實例
maven

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;
相關文章
相關標籤/搜索