pom.xml文件内容如下:
<dependencies>
<!-- fastdfs -->
<dependency>
<groupId>org.csource.fastdfs</groupId>
<artifactId>fastdfs</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
配置文件内容如下:
测试代码如下:
try {
// 1、加载配置文件,配置文件中的内容就是tracker服务的地址(绝对路径)
ClientGlobal.init("E:\\IdeaProjects\\weiku_rab\\test_fdfs\\src\\main\\resources\\fdfs_client.conf");
// 2、创建一个TrackerClient对象
TrackerClient trackerClient = new TrackerClient();
// 3、使用阿哥TrackerClient对象创建连接,获得一个TrackerServer对象
TrackerServer trackerServer = trackerClient.getConnection();
// 4、创建一个StorageServer的引用,值为null
StorageServer storageServer = null;
// 5、创建一个StorageClient对象,需要两个参数TrackerServer对象、Storage、Server的引用
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
// 6、使用StorageClient对象上传图片,扩展名不用带“.”
String [] strs = storageClient.upload_file("E:/bg2.jpg","jpg",null);
// 7、返回数组,包含组名和图片的路径
System.out.print("地址为:");
String path = "";
for (String str : strs) { // 组名+磁盘地址
path = path + str + "/";
}
// 进行地址处理并输出
System.out.println(path.substring(0,path.length()-1));
} catch (Exception e) {
e.printStackTrace();
}
运行结果如下: