FastDFSAttachmentServiceImpl.java
4.72 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
package com.hotent.file.attachmentService;
import cn.hutool.core.io.IoUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.FileInfo;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.hotent.base.attachment.Attachment;
import com.hotent.base.attachment.AttachmentService;
import com.hotent.base.attachment.MultipartFileParam;
import com.hotent.base.attachment.UploadShardResult;
import com.hotent.base.util.AppUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.*;
import java.util.Map;
@Service
public class FastDFSAttachmentServiceImpl implements AttachmentService {
@Resource
private FastFileStorageClient storageClient;
@Override
public String getStoreType() {
return "fastDFS";
}
private Logger logger = LoggerFactory.getLogger(FastDFSAttachmentServiceImpl.class);
@Override
public void remove(Attachment attachment, String propertiesId) throws Exception {
if (StringUtils.isEmpty(attachment.getFilePath())) {
return;
}
try {
StorePath storePath = StorePath.parseFromUrl(attachment.getFilePath());
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (FdfsUnsupportStorePathException e) {
logger.error("文件删除失败{}", e.getMessage());
throw e;
}
}
@Override
public void upload(Attachment attachment, InputStream inputStream, String propertiesId) throws Exception {
Long byteCount = attachment.getByteCount();
StorePath storePath = storageClient.uploadFile(inputStream, byteCount, attachment.getExtensionName(), null);
attachment.setFilePath(storePath.getFullPath());
System.out.println(storePath);
}
@Override
public void download(Attachment attachment, OutputStream outStream, String propertiesId) throws Exception {
String fileUrl = attachment.getFilePath();
String group = fileUrl.substring(0, fileUrl.indexOf("/"));
String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
DownloadByteArray downloadByteArray = new DownloadByteArray();
byte[] bytes = storageClient.downloadFile(group, path, downloadByteArray);
//将文件流写入到输出流中
IoUtil.write(outStream, true, bytes);
}
@Override
public boolean chekckFile(Attachment attachment, String propertiesId) throws Exception {
String fileUrl = attachment.getFilePath();
String group = fileUrl.substring(0, fileUrl.indexOf("/"));
String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
FileInfo fileInfo = storageClient.queryFileInfo(group, path);
return fileInfo != null;
}
@Override
public byte[] getFileBytes(Attachment sysFile) throws Exception {
String fileUrl = sysFile.getFilePath();
String group = fileUrl.substring(0, fileUrl.indexOf("/"));
String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
DownloadByteArray downloadByteArray = new DownloadByteArray();
byte[] bytes = storageClient.downloadFile(group, path, downloadByteArray);
return bytes;
}
@Override
public UploadShardResult uploadByShard(MultipartFileParam multipartFileParam, Attachment attachment, String propertiesId) throws Exception {
return null;
}
@Override
public Map<String, Object> generatePolicy(Attachment attachment, String propertiesId) {
return null;
}
@Override
public String getUrl(Attachment attachment, String propertiesId) throws UnsupportedEncodingException {
FdfsWebServer fdfsWebServer= AppUtil.getBean(FdfsWebServer.class);
String fileUrl = fdfsWebServer.getWebServerUrl() + attachment.getFilePath();
return fileUrl;
}
@Override
public InputStream getFileInputStream(Attachment attachment) throws FileNotFoundException {
String fileUrl = attachment.getFilePath();
String group = fileUrl.substring(0, fileUrl.indexOf("/"));
String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
DownloadByteArray downloadByteArray = new DownloadByteArray();
byte[] bytes = storageClient.downloadFile(group, path, downloadByteArray);
return new ByteArrayInputStream(bytes);
}
@Override
public String getFilePath(String account, String fileName) {
return null;
}
}