实现:自定义上传 M00/M01/M02 等路径
1.准备好 fdfs 服务,没有的话先搭建。点击这里
2.进入 storage 容器,修改配置文件/etc/fdfs/storage.conf(不同安装可能路径不一样)
# path(disk or mount point) count, default value is 1(挂载点数量,默认值为1)
store_path_count=2
# store_path#, based 0, if store_path0 not exists, it's value is base_path(如果store_path0不存在,将使用base_path路径)
# the paths must be exist (文件夹必须存在)
store_path0=/var/fdfs
store_path1=/var/fdfs/data2
这里修改 count 为 2 , 下边增加 store_path1=/var/fdfs/data2
3.然后呢?然后就结束了
4.使用 java-sdk 怎么上传指定路径
@Value("${fastdfs.tracker_servers}")
public void setTrackerServers(String tracker_servers){
try {
ClientGlobal.initByTrackers(tracker_servers);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
log.info("FastDFS.ClientGlobal.initByTrackers");
} catch (Exception e) {
log.error("FastDFS Client Init Fail!",e);
}
}
protected static TrackerClient trackerClient;
protected static TrackerServer trackerServer;
protected static StorageServer storageServer;
protected static StorageClient storageClient;
//文件上传
public static String uploadFile(byte[] file_buff, String fileExtName) throws IOException, MyException {
storageServer = trackerClient.getStoreStorage(trackerServer);
String storageIp = storageServer.getSocket().getInetAddress().getHostAddress();
Integer port = storageServer.getSocket().getPort();
//这里的第三个参数就是指定路径下标的,0->M00->store_path0 , 1->M01->store_path1 ...
storageServer = new StorageServer(storageIp, port, 1);
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
// 直接调用StorageClient对象方法上传文件即可。
String fileName = client.upload_file1(file_buff, fileExtName, null);
log.info("文件上传成功,路径为:{}" , fileName);
return fileName;
}
结果(到 M01 了)
INFO 48996 --- [ main] c.Y.common.util.dfs.FastDFSClientUtil : 文件上传成功,路径为:group1/M01/03/82/T489ul8IJ9OAHG8BAABmIZYby_8670.jpg
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于