|
| 1 | +package com.yiying.oss.util; |
| 2 | + |
| 3 | +import org.csource.common.NameValuePair; |
| 4 | +import org.csource.fastdfs.*; |
| 5 | +import org.springframework.core.io.ClassPathResource; |
| 6 | + |
| 7 | +import java.io.ByteArrayInputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | + |
| 11 | +/** |
| 12 | + * 描述 |
| 13 | + * |
| 14 | + * @since 1.0 |
| 15 | + */ |
| 16 | +public class FastDFSClient { |
| 17 | + |
| 18 | + static { |
| 19 | + //从classpath下获取文件对象获取路径 |
| 20 | + String path = new ClassPathResource("fdfs_client.conf").getPath(); |
| 21 | + try { |
| 22 | + ClientGlobal.init(path); |
| 23 | + } catch (Exception e) { |
| 24 | + e.printStackTrace(); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + //图片上传 |
| 29 | + public static String[] upload(FastDFSFile file) { |
| 30 | + try { |
| 31 | + TrackerClient trackerClient = new TrackerClient(); |
| 32 | + TrackerServer trackerServer = trackerClient.getConnection(); |
| 33 | + StorageClient storageClient = new StorageClient(trackerServer, null); |
| 34 | + //参数1 字节数组 |
| 35 | + //参数2 扩展名(不带点) |
| 36 | + //参数3 元数据( 文件的大小,文件的作者,文件的创建时间戳) |
| 37 | + NameValuePair[] meta_list = new NameValuePair[]{new NameValuePair(file.getAuthor()), new NameValuePair(file.getName())}; |
| 38 | + |
| 39 | + String[] strings = storageClient.upload_file(file.getContent(), file.getExt(), meta_list); |
| 40 | + |
| 41 | + return strings;// strings[0]==group1 strings[1]=M00/00/00/wKjThF1aW9CAOUJGAAClQrJOYvs424.jpg |
| 42 | + } catch (Exception e) { |
| 43 | + e.printStackTrace(); |
| 44 | + } |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + //图片下载 |
| 50 | + public static InputStream downFile(String groupName, String remoteFileName) { |
| 51 | + ByteArrayInputStream byteArrayInputStream = null; |
| 52 | + try { |
| 53 | + //3.创建trackerclient对象 |
| 54 | + TrackerClient trackerClient = new TrackerClient(); |
| 55 | + //4.创建trackerserver 对象 |
| 56 | + TrackerServer trackerServer = trackerClient.getConnection(); |
| 57 | + //5.创建stroageserver 对象 |
| 58 | + //6.创建storageclient 对象 |
| 59 | + StorageClient storageClient = new StorageClient(trackerServer, null); |
| 60 | + //7.根据组名 和 文件名 下载图片 |
| 61 | + |
| 62 | + //参数1:指定组名 |
| 63 | + //参数2 :指定远程的文件名 |
| 64 | + byte[] bytes = storageClient.download_file(groupName, remoteFileName); |
| 65 | + byteArrayInputStream = new ByteArrayInputStream(bytes); |
| 66 | + return byteArrayInputStream; |
| 67 | + } catch (Exception e) { |
| 68 | + e.printStackTrace(); |
| 69 | + } finally { |
| 70 | + try { |
| 71 | + if (byteArrayInputStream != null) { |
| 72 | + byteArrayInputStream.close(); |
| 73 | + } |
| 74 | + } catch (IOException e) { |
| 75 | + e.printStackTrace(); |
| 76 | + } |
| 77 | + } |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + //图片删除 |
| 83 | + |
| 84 | + public static void deleteFile(String groupName, String remoteFileName) { |
| 85 | + try { |
| 86 | + //3.创建trackerclient对象 |
| 87 | + TrackerClient trackerClient = new TrackerClient(); |
| 88 | + //4.创建trackerserver 对象 |
| 89 | + TrackerServer trackerServer = trackerClient.getConnection(); |
| 90 | + //5.创建stroageserver 对象 |
| 91 | + //6.创建storageclient 对象 |
| 92 | + StorageClient storageClient = new StorageClient(trackerServer, null); |
| 93 | + int i = storageClient.delete_file(groupName, remoteFileName); |
| 94 | + if (i == 0) { |
| 95 | + System.out.println("删除成功"); |
| 96 | + } else { |
| 97 | + System.out.println("删除失败"); |
| 98 | + } |
| 99 | + } catch (Exception e) { |
| 100 | + e.printStackTrace(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + //根据组名获取组的信息 |
| 105 | + |
| 106 | + public static StorageServer getStorages(String groupName) { |
| 107 | + try { |
| 108 | + TrackerClient trackerClient = new TrackerClient(); |
| 109 | + //4.创建trackerserver 对象 |
| 110 | + TrackerServer trackerServer = trackerClient.getConnection(); |
| 111 | + |
| 112 | + //参数1 指定traqckerserver 对象 |
| 113 | + //参数2 指定组名 |
| 114 | + StorageServer group1 = trackerClient.getStoreStorage(trackerServer, groupName); |
| 115 | + return group1; |
| 116 | + } catch (IOException e) { |
| 117 | + e.printStackTrace(); |
| 118 | + } |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + //根据文件名和组名获取文件的信息 |
| 123 | + |
| 124 | + public static FileInfo getFile(String groupName, String remoteFileName) { |
| 125 | + try { |
| 126 | + TrackerClient trackerClient = new TrackerClient(); |
| 127 | + //4.创建trackerserver 对象 |
| 128 | + TrackerServer trackerServer = trackerClient.getConnection(); |
| 129 | + |
| 130 | + StorageClient storageClient = new StorageClient(trackerServer, null); |
| 131 | + |
| 132 | + //参数1 指定组名 |
| 133 | + //参数2 指定文件的路径 |
| 134 | + FileInfo fileInfo = storageClient.get_file_info(groupName, remoteFileName); |
| 135 | + return fileInfo; |
| 136 | + } catch (Exception e) { |
| 137 | + e.printStackTrace(); |
| 138 | + } |
| 139 | + return null; |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + //根据文件名和组名 获取组信息的数组信息 |
| 144 | + public static ServerInfo[] getServerInfo(String groupName, String remoteFileName){ |
| 145 | + try { |
| 146 | + //3.创建trackerclient对象 |
| 147 | + TrackerClient trackerClient = new TrackerClient(); |
| 148 | + //4.创建trackerserver 对象 |
| 149 | + TrackerServer trackerServer = trackerClient.getConnection(); |
| 150 | + |
| 151 | + ServerInfo[] group1s = trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName); |
| 152 | + return group1s; |
| 153 | + } catch (IOException e) { |
| 154 | + e.printStackTrace(); |
| 155 | + } |
| 156 | + return null; |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + //获取tracker 的ip和端口的信息 |
| 161 | + //http://192.168.211.132:8080 |
| 162 | + public static String getTrackerUrl(){ |
| 163 | + try { |
| 164 | + //3.创建trackerclient对象 |
| 165 | + TrackerClient trackerClient = new TrackerClient(); |
| 166 | + //4.创建trackerserver 对象 |
| 167 | + TrackerServer trackerServer = trackerClient.getConnection(); |
| 168 | + //tracker 的ip的信息 |
| 169 | + String hostString = trackerServer.getInetSocketAddress().getHostString(); |
| 170 | + |
| 171 | + //http://192.168.211.132:8080/group1/M00/00/00/wKjThF1aW9CAOUJGAAClQrJOYvs424.jpg img |
| 172 | + int g_tracker_http_port = ClientGlobal.getG_tracker_http_port(); |
| 173 | + return "http://" + hostString + ":" + g_tracker_http_port; |
| 174 | + } catch (IOException e) { |
| 175 | + e.printStackTrace(); |
| 176 | + } |
| 177 | + return null; |
| 178 | + } |
| 179 | +} |
0 commit comments