Skip to content

Commit 2c34c31

Browse files
committed
fastdfs文件处理
1 parent 5f557b9 commit 2c34c31

File tree

5 files changed

+325
-2
lines changed

5 files changed

+325
-2
lines changed

luna-commons-file/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@
2929
<artifactId>jersey-client</artifactId>
3030
<version>1.19.1</version>
3131
</dependency>
32-
32+
<!-- fastDFS 文件服务-->
33+
<dependency>
34+
<groupId>net.oschina.zcx7878</groupId>
35+
<artifactId>fastdfs-client-java</artifactId>
36+
<version>1.27.0.0</version>
37+
</dependency>
3338
<dependency>
3439
<groupId>com.luna</groupId>
3540
<artifactId>luna-commons-common</artifactId>
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
package com.luna.file.fastDFS;
2+
3+
import com.luna.common.dto.constant.ResultCode;
4+
import com.luna.common.exception.FileException;
5+
import org.csource.common.NameValuePair;
6+
import org.csource.fastdfs.*;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.core.io.ClassPathResource;
10+
11+
import java.io.ByteArrayInputStream;
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
15+
/**
16+
* 描述
17+
*
18+
* @author luna
19+
* @version 1.0
20+
* @package com\luna\file\fastDFS
21+
* @since 1.0
22+
*/
23+
public class FastDFSClient {
24+
25+
private static final Logger log = LoggerFactory.getLogger(FastDFSClient.class);
26+
27+
static {
28+
// 从classpath下获取文件对象获取路径
29+
String path = new ClassPathResource("fdfs_client.conf").getPath();
30+
try {
31+
ClientGlobal.init(path);
32+
} catch (Exception e) {
33+
e.printStackTrace();
34+
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, "配置文件获取失败:" + e.getMessage());
35+
}
36+
}
37+
38+
/**
39+
* 图片上传
40+
*
41+
* @param file
42+
* @return
43+
*/
44+
public static String[] upload(FastDFSFile file) {
45+
try {
46+
TrackerClient trackerClient = new TrackerClient();
47+
TrackerServer trackerServer = trackerClient.getConnection();
48+
StorageClient storageClient = new StorageClient(trackerServer, null);
49+
// 参数1 字节数组
50+
// 参数2 扩展名(不带点)
51+
// 参数3 元数据( 文件的大小,文件的作者,文件的创建时间戳)
52+
NameValuePair[] meta_list =
53+
new NameValuePair[] {new NameValuePair(file.getAuthor()), new NameValuePair(file.getName())};
54+
55+
String[] strings = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
56+
57+
return strings;
58+
// strings[0]==group1 strings[1]=M00/00/00/wKjThF1aW9CAOUJGAAClQrJOYvs424.jpg
59+
} catch (Exception e) {
60+
e.printStackTrace();
61+
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, "图片上传失败:" + e.getMessage());
62+
}
63+
}
64+
65+
/**
66+
* 图片下载
67+
*
68+
* @param groupName 组名
69+
* @param remoteFileName 远程路径
70+
* @return
71+
*/
72+
public static InputStream downFile(String groupName, String remoteFileName) {
73+
ByteArrayInputStream byteArrayInputStream = null;
74+
try {
75+
// 3.创建trackerclient对象
76+
TrackerClient trackerClient = new TrackerClient();
77+
// 4.创建trackerserver 对象
78+
TrackerServer trackerServer = trackerClient.getConnection();
79+
// 5.创建stroageserver 对象
80+
// 6.创建storageclient 对象
81+
StorageClient storageClient = new StorageClient(trackerServer, null);
82+
// 7.根据组名 和 文件名 下载图片
83+
84+
// 参数1:指定组名
85+
// 参数2 :指定远程的文件名
86+
byte[] bytes = storageClient.download_file(groupName, remoteFileName);
87+
byteArrayInputStream = new ByteArrayInputStream(bytes);
88+
return byteArrayInputStream;
89+
} catch (Exception e) {
90+
e.printStackTrace();
91+
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, "图片下载失败:" + e.getMessage());
92+
} finally {
93+
try {
94+
if (byteArrayInputStream != null) {
95+
byteArrayInputStream.close();
96+
}
97+
} catch (IOException e) {
98+
e.printStackTrace();
99+
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, "系统异常,请重试:" + e.getMessage());
100+
}
101+
}
102+
}
103+
104+
/**
105+
* 图片删除
106+
*
107+
* @param groupName 组名
108+
* @param remoteFileName 远程路径
109+
*/
110+
public static void deleteFile(String groupName, String remoteFileName) {
111+
try {
112+
// 3.创建trackerclient对象
113+
TrackerClient trackerClient = new TrackerClient();
114+
// 4.创建trackerserver 对象
115+
TrackerServer trackerServer = trackerClient.getConnection();
116+
// 5.创建stroageserver 对象
117+
// 6.创建storageclient 对象
118+
StorageClient storageClient = new StorageClient(trackerServer, null);
119+
int i = storageClient.delete_file(groupName, remoteFileName);
120+
if (i == 0) {
121+
log.info("删除成功,groupName={},remoteFileName={}", groupName, remoteFileName);
122+
} else {
123+
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, "删除失败");
124+
}
125+
} catch (Exception e) {
126+
e.printStackTrace();
127+
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, "系统异常,请重试:" + e.getMessage());
128+
}
129+
}
130+
131+
/**
132+
* 根据组名获取组的信息
133+
*
134+
* @param groupName
135+
* @return
136+
*/
137+
public static StorageServer getStorages(String groupName) {
138+
try {
139+
TrackerClient trackerClient = new TrackerClient();
140+
// 4.创建trackerserver 对象
141+
TrackerServer trackerServer = trackerClient.getConnection();
142+
143+
// 参数1 指定traqckerserver 对象
144+
// 参数2 指定组名
145+
StorageServer group1 = trackerClient.getStoreStorage(trackerServer, groupName);
146+
return group1;
147+
} catch (IOException e) {
148+
e.printStackTrace();
149+
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, "根据组名获取组的信息失败:" + e.getMessage());
150+
}
151+
}
152+
153+
/**
154+
* 根据文件名和组名获取文件的信息
155+
*
156+
* @param groupName
157+
* @param remoteFileName
158+
* @return
159+
*/
160+
public static FileInfo getFile(String groupName, String remoteFileName) {
161+
try {
162+
TrackerClient trackerClient = new TrackerClient();
163+
// 4.创建trackerserver 对象
164+
TrackerServer trackerServer = trackerClient.getConnection();
165+
166+
StorageClient storageClient = new StorageClient(trackerServer, null);
167+
168+
// 参数1 指定组名
169+
// 参数2 指定文件的路径
170+
FileInfo fileInfo = storageClient.get_file_info(groupName, remoteFileName);
171+
return fileInfo;
172+
} catch (Exception e) {
173+
e.printStackTrace();
174+
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, "根据文件名和组名获取文件的信息失败:" + e.getMessage());
175+
}
176+
}
177+
178+
/**
179+
* 根据文件名和组名 获取组信息的数组信息
180+
*
181+
* @param groupName
182+
* @param remoteFileName
183+
* @return
184+
*/
185+
public static ServerInfo[] getServerInfo(String groupName, String remoteFileName) {
186+
try {
187+
// 3.创建trackerclient对象
188+
TrackerClient trackerClient = new TrackerClient();
189+
// 4.创建trackerserver 对象
190+
TrackerServer trackerServer = trackerClient.getConnection();
191+
192+
ServerInfo[] group1s = trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
193+
return group1s;
194+
} catch (IOException e) {
195+
e.printStackTrace();
196+
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, "根据文件名和组名 获取组信息的数组信息失败:" + e.getMessage());
197+
}
198+
}
199+
200+
/**
201+
* 获取tracker 的ip和端口的信息
202+
* http://47.92.89.8:8080
203+
*
204+
* @return
205+
*/
206+
public static String getTrackerUrl() {
207+
try {
208+
// 3.创建trackerclient对象
209+
TrackerClient trackerClient = new TrackerClient();
210+
// 4.创建trackerserver 对象
211+
TrackerServer trackerServer = trackerClient.getConnection();
212+
// tracker 的ip的信息
213+
String hostString = trackerServer.getInetSocketAddress().getHostString();
214+
215+
// http://47.92.89.8:8080/group1/M00/00/00/wKjThF1aW9CAOUJGAAClQrJOYvs424.jpg img
216+
int g_tracker_http_port = ClientGlobal.getG_tracker_http_port();
217+
return "http://" + hostString + ":" + g_tracker_http_port;
218+
} catch (IOException e) {
219+
e.printStackTrace();
220+
throw new FileException(ResultCode.ERROR_SYSTEM_EXCEPTION, "获取tracker 的ip和端口的信息失败:" + e.getMessage());
221+
}
222+
}
223+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.luna.file.fastDFS;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* 描述
7+
*
8+
* @author 三国的包子
9+
* @version 1.0
10+
* @package com.changgou.file *
11+
* @since 1.0
12+
*/
13+
public class FastDFSFile implements Serializable {
14+
15+
//文件名字
16+
private String name;
17+
//文件内容
18+
private byte[] content;
19+
//文件扩展名
20+
private String ext;
21+
//文件MD5摘要值
22+
private String md5;
23+
//文件创建作者
24+
private String author;
25+
26+
public FastDFSFile(String name, byte[] content, String ext, String md5, String author) {
27+
this.name = name;
28+
this.content = content;
29+
this.ext = ext;
30+
this.md5 = md5;
31+
this.author = author;
32+
}
33+
34+
public FastDFSFile(String name, byte[] content, String ext) {
35+
this.name = name;
36+
this.content = content;
37+
this.ext = ext;
38+
}
39+
40+
public FastDFSFile() {
41+
}
42+
43+
public String getName() {
44+
return name;
45+
}
46+
47+
public void setName(String name) {
48+
this.name = name;
49+
}
50+
51+
public byte[] getContent() {
52+
return content;
53+
}
54+
55+
public void setContent(byte[] content) {
56+
this.content = content;
57+
}
58+
59+
public String getExt() {
60+
return ext;
61+
}
62+
63+
public void setExt(String ext) {
64+
this.ext = ext;
65+
}
66+
67+
public String getMd5() {
68+
return md5;
69+
}
70+
71+
public void setMd5(String md5) {
72+
this.md5 = md5;
73+
}
74+
75+
public String getAuthor() {
76+
return author;
77+
}
78+
79+
public void setAuthor(String author) {
80+
this.author = author;
81+
}
82+
}

luna-commons-file/src/main/resources/application.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ spring:
33
active: dev
44

55
# 数据库
6-
6+
servlet:
7+
multipart:
8+
max-file-size: 10MB
9+
max-request-size: 10MB
710
logging:
811
level:
912
root: info
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#连接超时的世界 s
2+
connect_timeout=60
3+
#网络超时时间
4+
network_timeout=60
5+
#字符编码
6+
charset=UTF-8
7+
# tracker的http通信协议的端口
8+
http.tracker_http_port=8088
9+
# 22122 trackerserver的tcp 端口
10+
tracker_server=47.92.89.8:22122

0 commit comments

Comments
 (0)