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
package cn.wisenergy.chnmuseum.party.common.dfs;
import cn.wisenergy.chnmuseum.party.common.mvc.InterfaceException;
import cn.wisenergy.chnmuseum.party.common.util.CopyStreamUtils;
import cn.wisenergy.chnmuseum.party.common.util.FileTypeUtil;
import cn.wisenergy.chnmuseum.party.common.util.FileUtil;
import com.github.tobato.fastdfs.domain.fdfs.FileInfo;
import com.github.tobato.fastdfs.domain.fdfs.MetaData;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadFileStream;
import com.github.tobato.fastdfs.exception.FdfsServerException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@Slf4j
@Component
public class FastDFSUtils {
private static String dfsFileAccessBasePath;
private static ThumbImageConfig imageConfig;
private static FastFileStorageClient storageClient;
@Value("${dfsFileAccessBasePath:#{null}}")
public void setDfsFileAccessBasePath(String dfsFileAccessBasePath) {
FastDFSUtils.dfsFileAccessBasePath = dfsFileAccessBasePath;
}
@Resource
private ThumbImageConfig thumbImageConfig;
@Resource
private FastFileStorageClient fastFileStorageClient;
@PostConstruct
public void init() {
FastDFSUtils.storageClient = fastFileStorageClient;
FastDFSUtils.imageConfig = thumbImageConfig;
}
public static Map<String, Object> uploadUeImage(InputStream inputStream, long fileSize, String fileName) {
boolean isImage = FileTypeUtil.isImageByExtension(fileName);
if (isImage) {
String mimeType = FileUtil.getMimeType(fileName);
final String storePath = FastDFSUtils.uploadFile(inputStream, fileSize, fileName, null);
Map<String, Object> uploadResult = new HashMap<>();
uploadResult.put("url", storePath);
uploadResult.put("status", true);
uploadResult.put("message", "文件上传成功!");
uploadResult.put("title", fileName);
uploadResult.put("mimeType", mimeType);
uploadResult.put("size", fileSize);
return uploadResult;
}
throw new InterfaceException("400", "文件不是图片类型");
}
public static String uploadFile(InputStream inputStream, long size, String fileName, Set<MetaData> metaDataSet) {
final StorePath storePath = storageClient.uploadFile(inputStream, size, FilenameUtils.getExtension(fileName), metaDataSet);
return dfsFileAccessBasePath + "/" + storePath.getFullPath();
}
public static String uploadVideo(InputStream inputStream, long size, String fileName, Set<MetaData> metaDataSet) {
Map<String, Object> map = CopyStreamUtils.copyInputStream(inputStream);
String md5 = (String) map.get("md5");
metaDataSet.add(new MetaData("MD5", md5));
InputStream is = (InputStream) map.get("inputStream");
final StorePath storePath = storageClient.uploadFile(is, size, FilenameUtils.getExtension(fileName), metaDataSet);
return dfsFileAccessBasePath + "/" + storePath.getFullPath();
}
/**
* 使用 FastDFS 提供的客户端 storageClient 来进行文件上传,最后将上传结果返回。
* 根据 groupName 和文件名获取文件信息。
*/
public static FileInfo getFileInfo(String fileUrl) {
fileUrl = fileUrl.replace(dfsFileAccessBasePath + "/", "");
String groupName = fileUrl.substring(0, fileUrl.indexOf("/"));
String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
return storageClient.queryFileInfo(groupName, path);
}
/**
* 根据 groupName 和文件名获取文件信息。
*/
public static Set<MetaData> getFileMetaData(String fileUrl) {
fileUrl = fileUrl.replace(dfsFileAccessBasePath + "/", "");
String groupName = fileUrl.substring(0, fileUrl.indexOf("/"));
String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
return storageClient.getMetadata(groupName, path);
}
/**
* 下载文件(字节数组)
*/
public static BufferedInputStream downloadFile(String fileUrl, OutputStream outputStream) {
fileUrl = fileUrl.replace(dfsFileAccessBasePath + "/", "");
String groupName = fileUrl.substring(0, fileUrl.indexOf("/"));
String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
return storageClient.downloadFile(groupName, path, new DownloadFileStream(outputStream));
}
/**
* 下载文件
*/
public static InputStream downloadFile(String filePath) {
try {
StorePath storePath = StorePath.parseFromUrl(filePath);
return storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), inputStream -> inputStream);
} catch (FdfsServerException e) {
//不起作用
log.error("文件不存在,下载失败:" + e.getErrorCode());
throw new InterfaceException("文件不存在,下载失败:" + e.getErrorCode());
}
}
/**
* 删除文件
*/
public static void deleteFile(String filePath) {
StorePath storePath = StorePath.parseFromUrl(filePath);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
}
private static int[] getImageInfo(byte[] bytes) {
try {
ByteArrayInputStream baos = new ByteArrayInputStream(bytes);
BufferedImage image = ImageIO.read(baos);
int width = image.getWidth();
int height = image.getHeight();
return new int[]{width, height};
} catch (Exception e) {
log.error("FastDFSUtils.getImageInfo() error:", e);
}
return new int[]{0, 0};
}
private static ByteArrayOutputStream translateToByteArray(InputStream inputStream) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(inputStream, out);
return out;
}
// private static class UploadFileSender implements UploadCallback {
// private final InputStream is;
//
// public UploadFileSender(InputStream is) {
// this.is = is;
// }
//
// @Override
// public int send(OutputStream out) throws IOException {
// byte[] b = new byte[1024];
// int readBytes;
// while ((readBytes = is.read(b)) != -1) {
// out.write(b, 0, readBytes);
// }
// return 0;
// }
// }
}