FastDFSClientWrapper.java
4.11 KB
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
package com.glsx.publicservice.util.fastdfs;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.glsx.common.utils.PropertiesUtil;
import com.glsx.common.utils.StringUtils;
/**
*
* @Title FastDFSClientWrapper.java
* @Package com.qurong.credit.sp.utils.fastdfs
* @Description TODO fastdfs文件上传
* @author zhanghj
* @date 2018年7月27日 下午2:52:05
* @version
*/
@Component
public class FastDFSClientWrapper {
private static Logger logger = LoggerFactory.getLogger(FastDFSClientWrapper.class);
private static FastFileStorageClient storageClient;
// private static DefaultTrackerClient trackserClient;
// private static final String GENERAL_NGINX_PORT = "80";
private static final String FDFS_TRACKER_NGINX_DOMAIN = "fdfs_tracker_nginx_domain";
public static final String PROTOCOL = "http://";
public static final String SEPARATOR = "/";
/**
* 上传文件
*
* @param file
* 文件对象
* @return 文件访问地址
* @throws IOException
*/
public static String uploadFile(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()), null);
return getResAccessUrl(storePath);
}
/**
* 将一段字符串生成一个文件上传
*
* @param content
* 文件内容
* @param fileExtension
* @return
*/
public static String uploadFile(String content, String fileExtension) {
byte[] buff = content.getBytes(Charset.forName("UTF-8"));
ByteArrayInputStream stream = new ByteArrayInputStream(buff);
StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
return getResAccessUrl(storePath);
}
/**
* 将byte数组生成一个文件上传
*
* @param bytes 文件内容字节数组
* @param fileExtension
* @return
*/
public static String uploadFile(byte[] bytes, String fileExtension) {
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
StorePath storePath = storageClient.uploadFile(stream, bytes.length, fileExtension, null);
return getResAccessUrl(storePath);
}
// 封装图片完整URL地址
private static String getResAccessUrl(StorePath storePath) {
/*String fdfsTrackerNginxPort = PropertiesUtil.getProperty("fdfs_tracker_nginx_port");
String fileAbsolutePath = "";
if (GENERAL_NGINX_PORT.equals(fdfsTrackerNginxPort)) {
fileAbsolutePath = PROTOCOL + trackserClient.getStoreStorage().getIp() + SEPARATOR
+ storePath.getFullPath();
} else {
fileAbsolutePath = PROTOCOL + trackserClient.getStoreStorage().getIp() + ":" + fdfsTrackerNginxPort
+ SEPARATOR + storePath.getFullPath();
}*/
String domain = PropertiesUtil.getProperty(FDFS_TRACKER_NGINX_DOMAIN);
return domain + SEPARATOR + storePath.getFullPath();
}
/**
* 删除文件
*
* @param fileUrl
* 文件访问地址
* @return
*/
public static void deleteFile(String fileUrl) {
if (StringUtils.isEmpty(fileUrl)) {
return;
}
try {
StorePath storePath = StorePath.praseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (FdfsUnsupportStorePathException e) {
logger.warn(e.getMessage());
}
}
@Autowired
public void setStorageClient(FastFileStorageClient storageClient) {
FastDFSClientWrapper.storageClient = storageClient;
}
/*@Autowired
public void setTrackserClient(DefaultTrackerClient trackserClient) {
FastDFSClientWrapper.trackserClient = trackserClient;
}*/
}