liuxiaolong
2019-05-06 c15226e1b58f255dbebf1bdca8d4e53b9277249c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.basic.analy.utils;
 
 
//import org.csource.common.NameValuePair;
//import org.csource.fastdfs.*;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
//
//@Configuration
//public class FastDFSUtil2 {
//
//    //fdfs_client 核心配置文件
//    public static String conf_filename="fdfs_client.conf";
//
//
//    public  String  upload(MultipartFile file) {    //上传文件
//        TrackerServer trackerServer =null;
//        StorageServer storageServer = null;
//
//        try {
//            ClientGlobal.init(conf_filename);
//            TrackerClient tracker = new TrackerClient();
//            trackerServer = tracker.getConnection();
//
//            String ext_Name = file.getOriginalFilename().split("\\.")[1];
//
//            byte[] bytes = null;
//            try {
//                bytes = file.getBytes();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//            String fileIds[] = storageClient.upload_file(bytes, ext_Name, null);
//            return fileIds[0]+"/"+fileIds[1];
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "error";
//        }
//
//    }
//
//    public  String  uploadByByte(byte[] bytes,String extName) {    //上传文件
//        TrackerServer trackerServer =null;
//        StorageServer storageServer = null;
//
//        try {
//            ClientGlobal.init(conf_filename);
//            TrackerClient tracker = new TrackerClient();
//            trackerServer = tracker.getConnection();
//            StorageClient1 client = new StorageClient1(trackerServer, storageServer);
//
//            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//            String fileIds[] = storageClient.upload_file(bytes, extName, null);
//            return fileIds[0]+"/"+fileIds[1];
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "error";
//        }
//
//    }
//
//
//    public static void download() {    //下载文件
//        TrackerServer trackerServer =null;
//        StorageServer storageServer = null;
//
//        try {
//            String groupName = "group1";
//            String filePath = "/M00/00/18/wKgBtlvrelCAQCN2AAAlu9rpxmA717.jpg";
////            String filePath = "M00/00/00/ZGIW_lpujW-ADvpRAAblmT4ACuo125.png";
//            ClientGlobal.init(conf_filename);
//
//            TrackerClient tracker = new TrackerClient();
//            trackerServer = tracker.getConnection();
//
//            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//            byte[] bytes = storageClient.download_file(groupName, filePath);
//
//            String storePath = "/Users/shenwei/Desktop/download.png";
//            OutputStream out = new FileOutputStream(storePath);
//            out.write(bytes);
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally{
//            try {
//                if(null!=storageServer) storageServer.close();
//                if(null!=trackerServer) trackerServer.close();
//            } catch (IOException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
//        }
//    }
//
//
//    public  void getFileInfo(){ //获取文件信息
//        TrackerServer trackerServer =null;
//        StorageServer storageServer = null;
//
//        try {
//            String groupName = "group1";
//            String filePath = "M00/B6/ED/wKgBy1vYJrSAMlSiAACrc4GTmVg117.png";
//            ClientGlobal.init(conf_filename);
//
//            TrackerClient tracker = new TrackerClient();
//            trackerServer = tracker.getConnection();
//
//            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//            FileInfo file = storageClient.get_file_info(groupName, filePath);
//            System.out.println("ip--->"+file.getSourceIpAddr());
//            System.out.println("文件大小--->"+file.getFileSize());
//            System.out.println("文件上传时间--->"+file.getCreateTimestamp());
//            System.out.println(file.getCrc32());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    public  void getFileMate(){ //获取文件的原数据类型
//        TrackerServer trackerServer =null;
//        StorageServer storageServer = null;
//
//        try {
//            String groupName = "group1";
//            String filePath = "M00/00/00/ZGIW_lpujW-ADvpRAAblmT4ACuo125.png";
//            ClientGlobal.init(conf_filename);
//
//            TrackerClient tracker = new TrackerClient();
//            trackerServer = tracker.getConnection();
//
//            StorageClient storageClient = new StorageClient(trackerServer,
//                    storageServer);
//
//            //这个值是上传的时候指定的NameValuePair
//            NameValuePair nvps [] = storageClient.get_metadata(groupName, filePath);
//            if(null!=nvps && nvps.length>0){
//                for(NameValuePair nvp : nvps){
//                    System.out.println(nvp.getName() + ":" + nvp.getValue());
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    public  void delete(){ //删除文件
//        TrackerServer trackerServer =null;
//        StorageServer storageServer = null;
//
//        try {
//            String groupName = "group1";
//            String filePath = "M00/00/00/ZGIW_lpujW-ADvpRAAblmT4ACuo125.png";
//            ClientGlobal.init(conf_filename);
//
//            TrackerClient tracker = new TrackerClient();
//            trackerServer = tracker.getConnection();
//
//            StorageClient storageClient = new StorageClient(trackerServer,
//                    storageServer);
//            int i = storageClient.delete_file(groupName, filePath);
//            System.out.println( i==0 ? "删除成功" : "删除失败:"+i);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 根据图片的FastDFS地址获得数据的字节流
//     * @param strUrl 网络连接地址
//     * @return
//     */
//    public  byte[] getImageFromFDFS(String strUrl){
//        try {
//            URL url = new URL(strUrl);
//            System.out.println("url是:"+strUrl);
//            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//            conn.setRequestMethod("GET");
//            conn.setConnectTimeout(5 * 1000);
//            InputStream inStream = conn.getInputStream();//获取从网络的输入流
//            byte[] btImg = new byte[inStream.available()];
//            inStream.read(btImg);         //从输入流中得到图片的二进制数据
//            return btImg;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    public static byte[] downloadFDFS(String group_name,String remote_filename) throws Exception {
//        // 向项目中添加jar包
//        // 创建一个配置文件配置tracker服务器地址
//        //加载配置文件
//        ClientGlobal.init("fdfs_client.conf");
//
//        //创建一个tracker客户端对象
//        TrackerClient trackerClient = new TrackerClient();
//
//        //使用trackerclient连接trackersever对象
//        TrackerServer trackerServer = trackerClient.getConnection();
//
//        //创建一个straged server对象
//        StorageServer storageServer = null;
//
//        //创建一个storage client 对象
//        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//
//
//        byte[] download_file = storageClient.download_file(group_name,remote_filename);
//        //upload_file 存储着访问路径
//        return download_file;
//    }
//}