1 使用Fastdfs 的java客户端
在maven工程中使用jar包,需要添加依赖,不能直接添加jar包。
可以导入如下图所示的项目,并安装到本地仓库。
FastDFS-client的maven项目
第一步:导入,(maven install)并安装到本地仓库
第二步:copy 坐标到example_manager_web项目中的pom.xml中。
添加依赖
第三步:测试java代码:
Ø 加入测试依赖:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency>
|
Ø 创建目录结构为src/test/java,并设置如图所示的java目录为testresource目录
Ø 编写测试代码
第一步:创建配置文件fastdfs.conf ,并设置内容为:tracker_server=192.168.25.133:22122
第二步:加载全局的配置文件
第三步:创建trackerClient对象
第四步:创建trackerServer对象
第五步:创建storageServer对象设置为null即可
第六步:创建storageClient 的对象
第七步:使用storageClient对象上传图片
@Test public void testFastdfs() throws Exception{ // 第一步:创建配置文件fastdfs.conf ,并设置内容为:track_server=192.168.25.133:22122 // 第二步:加载全局的配置文件 ClientGlobal.init("C:\\Users\\ThinkPad\\IdeaProjects\\example_parent\\example_manager_web\\src\\main\\resources\\resources\\fastdfs.conf"); // 第三步:创建trackerClient对象 TrackerClient trackerClient = new TrackerClient(); // 第四步:创建trackerServer对象 TrackerServer trackerServer = trackerClient.getConnection(); // 第五步:创建storageServer对象设置为null即可 StorageServer storageServer =null; // 第六步:创建storageClient 的对象 StorageClient storageClient = new StorageClient(trackerServer,storageServer); // 第七步:使用storageClient对象上传图片 String[] strings = storageClient.upload_file("C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg", "jpg", null); for (String string : strings) { System.out.println(string); } }
|
配置文件如下:
2 使用fastdfs的工具类开发
添加如下代码放入example_manager_web工程下:
测试代码:
@Test publicvoidtestFastDfsClient() throws Exception { FastDFSClient fastDFSClient =new FastDFSClient("D:/workspaces-itcast/term197/example-manager-web/src/main/resources/resource/client.conf"); String file =fastDFSClient.uploadFile("D:/Documents/Pictures/images/2f2eb938943d.jpg"); System.out.println(file); } |