zhuo
9 天以前 fc543916cfac06f0cf16d018b9751417e3a119f7
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
package com.ruoyi.inspect.service.impl;
 
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.framework.exception.ErrorException;
import com.ruoyi.inspect.mapper.InsUnqualifiedHandlerFileMapper;
import com.ruoyi.inspect.pojo.InsUnqualifiedHandlerFile;
import com.ruoyi.inspect.service.InsUnqualifiedHandlerFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
 
/**
* @author 27233
* @description 针对表【ins_unqualified_handler_file(不合格处理附件记录表)】的数据库操作Service实现
* @createDate 2024-07-31 13:38:38
*/
@Service
@Slf4j
public class InsUnqualifiedHandlerFileServiceImpl extends ServiceImpl<InsUnqualifiedHandlerFileMapper, InsUnqualifiedHandlerFile>
    implements InsUnqualifiedHandlerFileService {
 
    @Value("${wordUrl}")
    private String wordUrl;
 
    @Value("${file.path}")
    private String imgUrl;
 
    @Override
    public InsUnqualifiedHandlerFile uploadFile(Long handlerId, MultipartFile file) {
        String urlString;
        String pathName;
        String path;
        String prefix;
        String filename = file.getOriginalFilename();
        String contentType = file.getContentType();
        InsUnqualifiedHandlerFile unqualifiedHandlerFile = new InsUnqualifiedHandlerFile();
//        unqualifiedHandlerFile.setUnqualifiedId(handlerId);
        unqualifiedHandlerFile.setFileName(filename);
        if (contentType != null && contentType.startsWith("image/")) {
            // 是图片
            path = imgUrl;
            prefix = "/image/";
            unqualifiedHandlerFile.setType(1);
        } else {
            // 是文件
            path = wordUrl;
            prefix = "/word/";
            unqualifiedHandlerFile.setType(2);
        }
        try {
            File realpath = new File(path);
            if (!realpath.exists()) {
                realpath.mkdirs();
            }
            pathName = UUID.randomUUID() + "_" + file.getOriginalFilename();
            urlString = realpath + "/" + pathName;
            file.transferTo(new File(urlString));
            unqualifiedHandlerFile.setFileUrl(pathName);
            baseMapper.insert(unqualifiedHandlerFile);
            return unqualifiedHandlerFile;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("附件上传错误");
            throw new ErrorException(e.getMessage());
        }
    }
 
    @Override
    public void downloadOAFile(Long handlerFileId, HttpServletResponse response) {
        response.reset();
        String fileName;
        try {
            //查询上传附件记录
            InsUnqualifiedHandlerFile file = baseMapper.selectById(handlerFileId);
            if (file != null){
                fileName = file.getFileName();
                fileName =  URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Cache-Control", "no-cache");
                response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
                response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
                //获取文件夹路径
                String path = file.getType()==1?imgUrl:wordUrl;
                //文件完整路径
                String fullPath = path + file.getFileUrl();
                //判断文件是否存在
                File fullFile = new File(fullPath);
                if(fullFile.exists()){
                    IoUtil.copy(Files.newInputStream(fullFile.toPath()),response.getOutputStream());
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
}