FdfsUtils.java
3.05 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
package cn.com.glsx.admin.modules.utils;
import cn.com.glsx.admin.config.FdfsPropertyConfig;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
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 java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
/**
* FastDFS 文件操作工具类
*
* @author yll
*/
@Component
public class FdfsUtils {
private static final String FDFS_DOMAIN_PROP = "tracker-nginx-domain";
private static final String DOMAIN_SEPARATOR = "/";
private static final Logger logger = LoggerFactory.getLogger(FdfsUtils.class);
private static FdfsPropertyConfig fdfsPropertyConfig;
private static FastFileStorageClient storageClient;
public static String uploadFile(MultipartFile multipartFile) throws IOException {
String fileUrl;
InputStream inputStream = multipartFile.getInputStream();
long fileSize = multipartFile.getSize();
String fileExtension = FilenameUtils.getExtension(multipartFile.getOriginalFilename());
StorePath storePath = storageClient.uploadFile(inputStream, fileSize, fileExtension,
null);
fileUrl = getFileUrl(storePath);
return fileUrl;
}
public static String uploadFile(byte[] bytes, String fileExtension) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
StorePath storePath = storageClient.uploadFile(inputStream, bytes.length, fileExtension,
null);
return getFileUrl(storePath);
}
public static String uploadFile(InputStream inputStream, String fileExtension)
throws IOException {
StorePath storePath = storageClient.uploadFile(inputStream, inputStream.available(),
fileExtension, null);
return getFileUrl(storePath);
}
public static void deleteFile(String fileUrl) {
if (StringUtils.isNoneBlank(fileUrl)) {
StorePath storePath = StorePath.parseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
}
}
private static String getFileUrl(StorePath storePath) {
String domain = fdfsPropertyConfig.getTrackerNginxDomain();
logger.info("domain == " + domain);
return domain + DOMAIN_SEPARATOR + storePath.getFullPath();
}
@Autowired
public void setFdfsProperty(FdfsPropertyConfig fdfsPropertyConfig) {
FdfsUtils.fdfsPropertyConfig = fdfsPropertyConfig;
}
@Autowired
public void setStorageClient(FastFileStorageClient storageClient) {
FdfsUtils.storageClient = storageClient;
}
public static String getFileName(String desName) {
return desName + DateUtil.formatSerial(new Date());
}
}