上传图片到远程服务器
// 1、把FastDFS提供的jar包添加到工程中
// 2、初始化全局配置。加载一个配置文件。
ClientGlobal.init("D:\\projectworkspace\\FastDfs\\src\\cn\\jiminmin\\com\\fdfs_client.conf");
// 3、创建一个TrackerClient对象。
TrackerClient trackerClient = new TrackerClient();
// 4、创建一个TrackerServer对象。
TrackerServer trackerServer = trackerClient.getConnection();
// 5、声明一个StorageServer对象,null。
StorageServer storageServer = null;
// 6、获得StorageClient对象。
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
// 7、直接调用StorageClient对象方法上传文件即可。
String[] strings = storageClient.upload_file("F:\\images\\1.jpg", "jpg", null);
for (String string : strings) {
System.out.println(string); //group1/M00/00/00/rBGNeFyY0pKATRfNAABk1pD11A4995.jpg
}
备注:fdfs_client.conf 中的内容:
下载storage 的图片到本地任意路径:
// 1、把FastDFS提供的jar包添加到工程中
// 2、初始化全局配置。加载一个配置文件。
ClientGlobal.init("D:\\projectworkspace\\FastDfs\\src\\cn\\jiminmin\\com\\fdfs_client.conf");
// 3、创建一个TrackerClient对象。
TrackerClient trackerClient = new TrackerClient();
// 4、创建一个TrackerServer对象。
TrackerServer trackerServer = trackerClient.getConnection();
// 5、声明一个StorageServer对象,null。
StorageServer storageServer = null;
// 6、获得StorageClient对象。
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
byte[] file_buff = storageClient.download_file("group1", "M00/00/00/rBGNeFyY0pKATRfNAABk1pD11A4995.jpg");
File file = new File("WebRoot/img/111.jpg");
FileOutputStream fileOutPutStream = new FileOutputStream(file);
fileOutPutStream.write(file_buff);
fileOutPutStream.flush();
fileOutPutStream.close();
删除 文件
TrackerServer trackerServer = null;
StorageServer storageServer = null;
try {
ClientGlobal.init("D:\\projectworkspace\\FastDfs\\src\\cn\\jiminmin\\com\\fdfs_client.conf");
TrackerClient trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
int i = storageClient.delete_file("group1", "M00/00/00/wKgBOFy635yAJtquAABk1pD11A4243.jpg");
System.out.println(i == 0 ? "删除成功" : "删除失败"+i); // 删除成功之后 也许还可以访问,删除浏览器 缓存 ,就好了
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(trackerServer != null){
System.out.println("guanbi");
trackerServer.close();
}
}