yangys
2025-09-22 edecbd9cb1b31ded46b8592634780d999db6b6f0
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 org.springblade.mdm.commons.service;
 
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.RuntimeService;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BizServiceImpl;
import org.springblade.core.oss.OssTemplate;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.tool.utils.UrlUtil;
import org.springblade.mdm.flow.entity.FlowProgramFile;
import org.springblade.mdm.flow.mapper.FlowProgramFileMapper;
import org.springblade.mdm.flow.service.FlowCommonService;
import org.springblade.mdm.flow.service.FlowProgramProperties;
import org.springblade.mdm.flow.vo.ProgramUploadVO;
import org.springblade.mdm.program.service.NcNodeService;
import org.springblade.mdm.utils.FileContentUtil;
import org.springblade.mdm.utils.ProgramFileNameCheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
 
/**
 * OSS文件通用服务,包括下载,返回文件内容等
 *
 * @author yangys
 */
@Slf4j
@Service
public class OssFileCommonService{
    @Autowired
    private OssTemplate ossTemplate;
 
 
    /**
     * 获取文件内容
     * @param ossFileName 文件id
     * @return 文件内容文本
     */
    public String getOssFileContent(String ossFileName) {
        String result;
        try (InputStream inputStream = ossTemplate.statFileStream(ossFileName)) {
            ByteArrayInputStream bos = new ByteArrayInputStream(inputStream.readAllBytes());
            boolean isText = StringUtils.endsWithIgnoreCase(ossFileName,".txt") || StringUtils.endsWithIgnoreCase(ossFileName,".nc")|| StringUtils.endsWithIgnoreCase(ossFileName,".xml");
            if(!isText){
                isText= FileContentUtil.isTextFile(bos);
            }
            if(isText){
                bos.reset();
                result = FileContentUtil.getContentFromStream(bos);
            }else{
                result = "<非文本文件>";
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
 
        return result;
    }
 
    /**
     * 从oss下载文件
     * @param ossFileName oss的文件名
     * @param downloadFileName 指定下载的文件名
     * @param response http相应
     * @throws IOException 操作文件IO 异常
     */
    public void download(String ossFileName, String downloadFileName,HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition", "attachment; filename="+ UrlUtil.encode(downloadFileName));
        response.setContentType("application/octet-stream");
        try(InputStream ins = ossTemplate.statFileStream(ossFileName);){
            IOUtils.copy(ins,response.getOutputStream());
        }
    }
 
 
}