spring集成:如何用传统方式使用fastDFSClient

来源:互联网 发布:stc12c5a16s2单片机 编辑:程序博客网 时间:2024/06/05 19:14

最近一直在摸索如何使用带有连接池的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集成
阅读全文
0 0