fastdfs分布式文件存储

    科技2025-05-06  57

    文章目录

    pom引入fdfs_client.confDfsUtil.java文件上传、下载、删除、获取元数据

    pom引入

    <dependencies> <dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> </dependencies>

    fdfs_client.conf

    connect_timeout = 2 network_timeout = 30 charset = UTF-8 http.tracker_http_port = 8111 tracker_server = 192.168.20.252:22122

    DfsUtil.java

    package com.javasm; import org.csource.common.NameValuePair; import org.csource.fastdfs.*; import java.io.IOException; import java.net.URL; import java.util.HashMap; import java.util.Map; public class DfsUtil { private static TrackerClient client =null; public static final String FILE_SIZE="file_size"; public static final String UPLOAD_TIME="upload_time"; public static final String REL_NAME="rel_name"; static{ // file:/E:/workspace/three/0823fastdfs/target/classes/fdfs_client.conf URL resource = DfsUtil.class.getClassLoader().getResource("fdfs_client.conf"); // /E:/workspace/three/0823fastdfs/target/classes/fdfs_client.conf String confFileName = resource.getPath(); //fastdfs的api try { ClientGlobal.init(confFileName);//把配置信息保存到了ClientGlobal的静态类变量中。 client = new TrackerClient();//初始化跟踪服务器的客户端api对象,该对象中有所有的跟踪服务器的具体地址信息。 } catch (Exception e) { e.printStackTrace(); } } /** * springMVC中文件表单对象:MulipartFile getBytes() getInputStream() */ public static String upload(byte[] bytes,long size,String fileName){ String result = null; TrackerServer ts = null;//跟踪服务器 StorageServer ss = null;//存储服务器 try { ts = client.getConnection();//连接上一个具体的跟踪服务器 StorageClient1 storageClient1 = new StorageClient1(ts,ss); String extName = fileName.substring(fileName.lastIndexOf(".")+1); NameValuePair[] meta_list = new NameValuePair[]{ new NameValuePair(FILE_SIZE,size+""), new NameValuePair(REL_NAME,fileName), new NameValuePair(UPLOAD_TIME,System.currentTimeMillis()+"") }; //文件内容,后缀名,元数据列表(上传人,上传时间,文件真实名,文件大小),返回文件的存储路径(/组名/路径/文件名) result = storageClient1.upload_file1(bytes, extName, meta_list); return result; } catch (Exception e) { e.printStackTrace(); } finally { if(ss!=null){ try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } if(ts!=null){ try { ts.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } public static byte[] downLoad(String filePath) { TrackerServer ts = null;//跟踪服务器 StorageServer ss = null;//存储服务器 try { ts = client.getConnection();//连接上一个具体的跟踪服务器 StorageClient1 storageClient1 = new StorageClient1(ts,ss); byte[] bytes = storageClient1.download_file1(filePath); return bytes; } catch (Exception e) { e.printStackTrace(); } finally { if(ss!=null){ try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } if(ts!=null){ try { ts.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } public static long getUploadTime(String filePath){ return Long.parseLong(getMetaDatas(filePath).get(UPLOAD_TIME)); } public static String getFileSize(String filePath){ return getMetaDatas(filePath).get(FILE_SIZE); } public static String getFileName(String filePath){ return getMetaDatas(filePath).get(REL_NAME); } public static Map<String,String> getMetaDatas(String filePath) { TrackerServer ts = null;//跟踪服务器 StorageServer ss = null;//存储服务器 try { ts = client.getConnection();//连接上一个具体的跟踪服务器 StorageClient1 storageClient1 = new StorageClient1(ts,ss); Map<String,String> metas = new HashMap<>(); NameValuePair[] metadata1 = storageClient1.get_metadata1(filePath); if(metadata1!=null){ for(NameValuePair nv:metadata1){ metas.put(nv.getName(),nv.getValue()); } return metas; } } catch (Exception e) { e.printStackTrace(); } finally { if(ss!=null){ try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } if(ts!=null){ try { ts.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } public static boolean delFile(String filePath) { TrackerServer ts = null;//跟踪服务器 StorageServer ss = null;//存储服务器 try { ts = client.getConnection();//连接上一个具体的跟踪服务器 StorageClient1 storageClient1 = new StorageClient1(ts,ss); int result = storageClient1.delete_file1(filePath); return result==0?true:false; } catch (Exception e) { e.printStackTrace(); } finally { if(ss!=null){ try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } if(ts!=null){ try { ts.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; } public static String getHttpUrl(String filePath) { TrackerServer ts = null;//跟踪服务器 try { ts = client.getConnection();//连接上一个具体的跟踪服务器 String hostName = ts.getInetSocketAddress().getHostName(); return "http://"+hostName+":"+ClientGlobal.getG_tracker_http_port()+"/"+filePath; } catch (Exception e) { e.printStackTrace(); } finally { if(ts!=null){ try { ts.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } }

    文件上传、下载、删除、获取元数据

    package com.javasm; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.util.Map; /** * 针对的上传的图片,要在浏览器上展示: * 针对的是文件下载:上传,下载,删除,获取元数据 * */ public class App { public static void main( String[] args ) { //group1/M00/00/12/wKgU_F808UmAJLTzAAD_2Eqxn3g488.png //upload(); getHttpUrl(); } public static void getHttpUrl(){ String filePath = "group1/M00/00/12/wKgU_F808UmAJLTzAAD_2Eqxn3g488.png"; String httpurl = DfsUtil.getHttpUrl(filePath); System.out.println(httpurl); } public static void delFile(){ String filePath = "group1/M00/00/12/wKgU_F807QuAcM4tAAAnBdx6rQA75.xlsx"; boolean isDel = DfsUtil.delFile(filePath); } public static void getMetaDatas(){ String filePath = "group1/M00/00/12/wKgU_F807QuAcM4tAAAnBdx6rQA75.xlsx"; String fileName = DfsUtil.getFileName(filePath); System.out.println(fileName); } public static void down(){ String filePath = "group1/M00/00/12/wKgU_F807QuAcM4tAAAnBdx6rQA75.xlsx"; byte[] bytes = DfsUtil.downLoad(filePath); File file = new File("D:/xxx.xlsx"); try { FileUtils.writeByteArrayToFile(file,bytes); } catch (IOException e) { e.printStackTrace(); } } public static void upload(){ String str = "D:/无标题.png"; File file = new File(str); String name = file.getName(); long size = file.length(); try { byte[] bytes = FileUtils.readFileToByteArray(file);//把文件对象中的数据读取到字节数组中 String upload = DfsUtil.upload(bytes, size, name); System.out.println(upload); } catch (IOException e) { e.printStackTrace(); } } }
    Processed: 0.011, SQL: 8