搭建fastDfs服务端请参考 http://www.imooc.com/article/66981?block_id=tuijian_wz
导入fastdfs客户端依赖包
<dependency> <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27.0.0</version> </dependency>
StorageClient的代理类BeeStorageClient
package com.declanwu.pub.fastdfs.client; import org.csource.common.MyException; import org.csource.fastdfs.StorageClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import java.io.IOException; public class BeeStorageClient { private Logger logger = LoggerFactory.getLogger(this.getClass()); private StorageClient storageClient; @Value("${fastdfs.storage_server}") private String storage_server; public BeeStorageClient(StorageClient storageClient) throws IOException, MyException { this.storageClient = storageClient; } /** * upload file to storage server (by file name) * * @param local_filename local filename to upload * @param file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename * @return 2 elements string array if success: * <ul><li>results[0]: the group name to store the file </li></ul> * <ul><li>results[1]: the new created filename</li></ul> * return null if fail */ public String[] uploadFile(String local_filename, String file_ext_name) throws IOException, MyException { String[] strings; try { strings = storageClient.upload_file(local_filename, file_ext_name, null); } catch (IOException e) { // Avoid java.io.IOException: recv package size -1 != 10 strings = storageClient.upload_file(local_filename, file_ext_name, null); } return strings; } /** * download file from storage server * * @param group_name the group name of storage server * @param remote_filename filename on storage server * @return file content/buff, return null if fail */ public byte[] downloadFile(String group_name, String remote_filename) throws IOException, MyException { return storageClient.download_file(group_name, remote_filename); } public String getFileUrl(String groupName, String remoteFilename) { return this.storage_server + "/" + groupName + "/" + remoteFilename; } }
初始化BeeStorageClient,由spring进行管理
package com.declanwu.pub.fastdfs.init;
import com.declanwu.pub.fastdfs.client.BeeStorageClient;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Properties; @Configuration public class InitConfig { private Logger logger = LoggerFactory.getLogger(this.getClass()); private final static int connect_timeout_in_seconds = 30; private final static int network_timeout_in_seconds = 60; // private final static String tracker_servers = "10.192.10.60:22122"; private final static int http_tracker_http_port = 80; @Value("${fastdfs.tracker_servers}") private String tracker_servers; @Bean("beeStorageClient") public BeeStorageClient storageClientConfiguration() throws Exception { Properties properties = new Properties(); properties.put("fastdfs.connect_timeout_in_seconds", connect_timeout_in_seconds); properties.put("fastdfs.network_timeout_in_seconds", network_timeout_in_seconds); properties.put("fastdfs.tracker_servers", tracker_servers); properties.put("fastdfs.http_tracker_http_port", http_tracker_http_port); // 1、加载配置,配置中的内容就是 tracker 服务的地址。 ClientGlobal.initByProperties(properties); // 2、创建一个 TrackerClient 对象。直接 new 一个。 TrackerClient trackerClient = new TrackerClient(); // 3、使用 TrackerClient 对象创建连接,获得一个 TrackerServer 对象。 TrackerServer trackerServer = trackerClient.getConnection(); // 4、创建一个 StorageServer 的引用,值为 null StorageServer storageServer = null; // 5、创建一个 StorageClient 对象,需要两个参数 TrackerServer 对象、StorageServer 的引用 StorageClient storageClient = new StorageClient(trackerServer, storageServer); return new BeeStorageClient(storageClient); } }
bootstrap.yum中动态设置tracker服务地址和storage的展示地址
fastdfs:
tracker_servers: ${app.fastdfs.tracker.server:192.168.0.100:22122}
storage_server: ${app.fastdfs.storage.server:http://192.168.0.100:8080}
调用方式
...
@Autowired
private BeeStorageClient beeStorageClient;
...
String[] strings = beeStorageClient.uploadFile("C:\\Users\\Administrator\\Pictures\\timg.jpg", "jpg");
System.out.println(beeStorageClient.getFileUrl(strings[0], strings[1]));
...