DatabaseAttachmentServiceImpl.java
3.2 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
package com.hotent.file.attachmentService;
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.FileUtil;
import com.hotent.file.util.AppFileUtil;
import org.springframework.stereotype.Service;
import java.io.*;
import java.util.Map;
@Service
public class DatabaseAttachmentServiceImpl implements AttachmentService{
public void remove(Attachment sysFile,String propertiesId) throws Exception {
// 数据库存放附件模式,直接删除数据库中的记录即可
}
public void upload(Attachment sysFile, InputStream inputStream,String propertiesId) throws Exception {
// 上传附件时,将附件字节流设置到SysFile的FileBlob属性中
sysFile.setBytes(FileUtil.readByte(inputStream));
}
public void download(Attachment sysFile, OutputStream outStream,String propertiesId) throws Exception {
//获取附件字节数组
if(Attachment.SAVE_TYPE_FOLDER.equals(sysFile.getStoreType())){
/* 根据文件路径获取文件 */
File file = new File(AppFileUtil.getAttachPath()+File.separator+sysFile.getFilePath());
if (file.exists()) { // 文件存在
/* 根据已存在的文件,创建文件输入流 */
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(file));){
/* 创建缓冲区,大小为流的最大字符数 */
byte[] buffer = new byte[inputStream.available()]; // int available() 返回值为流中尚未读取的字节的数量
/* 从文件输入流读字节流到缓冲区 */
inputStream.read(buffer);
/* 关闭输入流 */
outStream.write(buffer);
/* 刷空输出流,并输出所有被缓存的字节 */
outStream.flush();
} catch (Exception e) {
} finally {
if (outStream != null) {
outStream.close();
}
}
}
}else if(Attachment.SAVE_TYPE_FTP.equals(sysFile.getStoreType())) {
}else if(Attachment.SAVE_TYPE_ALIYUN_OSS.equals(sysFile.getStoreType())) {
}else{
byte[] fileBlob = sysFile.getBytes();
try{
outStream.write(fileBlob);
outStream.flush();
}
catch(Exception e){
throw e;
}
finally{
if (outStream != null) {
outStream.close();
}
}
}
}
@Override
public String getStoreType() {
return "database";
}
@Override
public boolean chekckFile(Attachment attachment,String propertiesId) {
return true;
}
@Override
public byte[] getFileBytes(Attachment sysFile) {
return sysFile == null? null : sysFile.getBytes();
}
@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 {
return null;
}
@Override
public InputStream getFileInputStream(Attachment attachment) throws FileNotFoundException {
return null;
}
@Override
public String getFilePath(String account, String fileName) {
return null;
}
}