1
hao
2025-05-20 8e24c6fea30d9b179375ee2893710cdec2443b13
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
175
176
177
package com.system.file.service.internal;
 
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.util.Random;
 
import javax.servlet.http.HttpServletResponse;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.multipart.MultipartFile;
 
import com.app.base.data.ApiResponseResult;
import com.app.base.service.FtpClientService;
import com.system.file.dao.FsFileDao;
import com.system.file.entity.FsFile;
import com.system.file.service.FileService;
 
@Configuration
public class FileImpl  implements FileService {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private FsFileDao fsFileDao;
    @Autowired
    private FtpClientService ftpClientService;
    @Autowired
    private Environment env;
 
    /**
     * 上传文件
     * @param fsFile
     * @param file
     * @return
     * @throws Exception
     */
    public ApiResponseResult upload(FsFile fsFile, MultipartFile file) throws Exception {
        if(null==file || file.isEmpty()) {
            return ApiResponseResult.failure("上传文件不能为空");
        }
        String qmsPath = env.getProperty("fs.qms.path");
 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String ymd = sdf.format(new Date());
 
        String path = qmsPath + "/" + ymd;
 
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateFileName = df.format(new Date()) + "_" + new Random().nextInt(1000);
 
        try {
            fsFile.setBsFileSize(file.getSize());
            if(null==fsFile.getBsContentType()) {
                fsFile.setBsContentType(file.getContentType());
            }
            if(null==file.getOriginalFilename()) {
                fsFile.setBsFileType("Unknown");
                return ApiResponseResult.failure("无法识别该文件类型!");
            }
 
            String originalFiletype = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."), file.getOriginalFilename().length());
            fsFile.setBsFileType(originalFiletype);
 
//            String originalFilename = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."));
            fsFile.setBsName(file.getOriginalFilename());
            fsFile.setBsFileName(dateFileName + originalFiletype);
            fsFile.setBsFilePath("/"+ymd);
            ApiResponseResult result = ftpClientService.uploadFile(path, dateFileName+fsFile.getBsFileType(), new ByteArrayInputStream(file.getBytes()));
            if(result.isResult()) {
                fsFile.setCreatedTime(new Date());
                fsFileDao.save(fsFile);
                return ApiResponseResult.success("文件上传成功!").data(fsFile);
            }
        } catch (IOException e) {
            logger.error("upload file exception", e);
        }
        return ApiResponseResult.failure("上传文件发生异常");
    }
 
    /**
     * 下载文件
     * @param fsFileId
     * @param response
     * @return
     * @throws Exception
     */
    public ApiResponseResult get(Long fsFileId, HttpServletResponse response) throws Exception {
        Optional<FsFile> fsFiles = fsFileDao.findById(fsFileId);
        if(null==fsFiles) {
            return ApiResponseResult.failure("文件不存在或已被删除");
        }
        FsFile fsFile = fsFiles.get();
        String path = env.getProperty("fs.qms.path")+fsFile.getBsFilePath();
        ApiResponseResult result = ftpClientService.download(path, fsFile.getBsFileName());
        try {
//            String fileName = new String(fsFile.getBsName().getBytes("UTF-8"), "ISO-8859-1")+ fsFile.getBsFileType();
            String fileName = URLEncoder.encode(fsFile.getBsName(), "UTF-8");
//            response.setContentType("application/octet-stream");
            response.setContentType(fsFile.getBsContentType());
            // 设置response的Header
//            response.setHeader("Content-Disposition", "attachment;filename=" + new String((fsFile.getBsName()+fsFile.getBsFileType()).getBytes("UTF-8"), "ISO-8859-1"));
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName );
            response.addHeader("Content-Length", "" + fsFile.getBsFileSize());
            OutputStream os = response.getOutputStream();
            byte[] bytes = (byte[]) result.getData();
            os.write(bytes);
            os.flush();
            os.close();
        } catch (IOException e) {
            logger.error("download file exception", e);
        }
        return null;
    }
 
    /**
     * 图片在线预览(非图片下载)
     * @param fsFileId
     * @param response
     * @return
     * @throws Exception
     */
    public ApiResponseResult onlineView(Long fsFileId, HttpServletResponse response) throws Exception {
        Optional<FsFile> fsFiles = fsFileDao.findById(fsFileId);
        if(null==fsFiles) {
            return ApiResponseResult.failure("文件不存在或已被删除");
        }
        FsFile fsFile = fsFiles.get();
        String path = env.getProperty("fs.qms.path")+fsFile.getBsFilePath();
        ApiResponseResult result = ftpClientService.download(path, fsFile.getBsFileName());
        try {
            String fileName = URLEncoder.encode(fsFile.getBsName(), "UTF-8");  //文件名称
            String extName = fsFile.getBsFileType();  //文件后缀名
            response.setContentType(fsFile.getBsContentType());
            response.addHeader("Content-Disposition", "inline;filename=" + fileName );
            response.addHeader("Content-Length", "" + fsFile.getBsFileSize());
//            if(".png".equals(extName)){
//                response.setContentType("image/png");
//            }
            OutputStream os = response.getOutputStream();
            byte[] bytes = (byte[]) result.getData();
            os.write(bytes);
            os.flush();
            os.close();
        } catch (IOException e) {
            logger.error("download file exception", e);
        }
        return null;
    }
 
    /**
     * 删除文件
     * @param fsFileId
     * @return
     * @throws Exception
     */
    public ApiResponseResult delete(Long fsFileId) throws Exception{
        if(fsFileId == null){
            return ApiResponseResult.failure("记录ID不能为空!");
        }
        Optional<FsFile> fsFiles = fsFileDao.findById(fsFileId);
        if(null==fsFiles) {
            return ApiResponseResult.failure("文件不存在或已被删除");
        }
        FsFile fsFile = fsFiles.get();
 
        fsFile.setIsDel(1);
        fsFileDao.save(fsFile);
        return ApiResponseResult.success("文件删除成功!");
    }
}