zss
2025-03-07 12291480d592402398c3ba76e8de6006da5c2612
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
package com.ruoyi.personnel.service.impl;
 
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.personnel.enumeration.AttachmentType;
import com.ruoyi.personnel.mapper.PersonTrainingFileMapper;
import com.ruoyi.personnel.pojo.PersonTrainingFile;
import com.ruoyi.personnel.service.FileGeneralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
 
@Service
 
public class FileGeneralServiceImpl implements FileGeneralService {
 
    @Autowired
    private PersonTrainingFileMapper fileMapper;
 
    @Value("${wordUrl}")
    private String wordUrl;
 
    @Value("${file.path}")
    private String imgUrl;
 
    @Value("${excelUrl}")
    private String excelUrl;
 
    @Override
    public void fileUpload(MultipartFile file, String suffix, Integer id,Integer type) {
        String name = file.getOriginalFilename();
        UUID uuid = UUID.randomUUID();
        String fileName = uuid + name;
        String path = "";
        // 根据后缀判断属于什么类型
        if(suffix.toLowerCase().contains("xls")) {
            path = excelUrl;
        } else if (suffix.toLowerCase().contains("doc") || suffix.toLowerCase().contains("pdf")) {
            path = wordUrl;
        } else {
            path = imgUrl;
        }
        File file1 = new File(path);
        if(!file1.exists()) {
            file1.mkdir();
        }
        try {
            File file2 = new File(file1, fileName);
            file.transferTo(file2);
            // 保存数据库
            PersonTrainingFile personTrainingFile = new PersonTrainingFile();
            personTrainingFile.setDetailId(id);
            personTrainingFile.setFileUrl("/" + fileName);
            personTrainingFile.setFileName(file.getOriginalFilename());
            personTrainingFile.setCreateUser(SecurityUtils.getUserId().intValue());
            personTrainingFile.setCreateTime(LocalDateTime.now());
            personTrainingFile.setEnumAttachmentType(AttachmentType.getTypeValue(type));
            fileMapper.insert(personTrainingFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
    @Override
    public List<PersonTrainingFile> selectFile(Integer id, Integer type) {
        List<PersonTrainingFile> personTrainingFileList = fileMapper.selectList(new LambdaQueryWrapper<PersonTrainingFile>()
                .eq(PersonTrainingFile::getDetailId, id)
                .eq(PersonTrainingFile::getEnumAttachmentType, AttachmentType.getTypeValue(type)));
        if(CollectionUtils.isNotEmpty(personTrainingFileList)) {
            for(PersonTrainingFile a : personTrainingFileList) {
                // 设置mime
                int i = a.getFileUrl().lastIndexOf(".");
                String contentType = getContentType(a.getFileUrl().substring(i + 1));
                a.setMime(contentType);
            }
        }
        return personTrainingFileList;
    }
 
    @Override
    public void delFile(Integer id) {
        fileMapper.deleteById(id);
    }
 
    @Override
    public void fileDownLoad(Integer id, HttpServletResponse response) {
        PersonTrainingFile personTrainingFile = fileMapper.selectById(id);
        // 获取文件后缀
        String suffix = personTrainingFile.getFileName().substring(personTrainingFile.getFileName().lastIndexOf(".") + 1);
        String path = "";
        if(suffix.toLowerCase().contains("xls")) {
            path = excelUrl;
        } else if (suffix.toLowerCase().contains("doc") || suffix.toLowerCase().contains("pdf")) {
            path = wordUrl;
        } else {
            path = imgUrl;
        }
        File file = new File(path + File.separator + personTrainingFile.getFileUrl());
        if(!file.exists()) {
            throw new RuntimeException("文件不存在");
        }
        try(FileInputStream fileInputStream = new FileInputStream(file);
            ServletOutputStream stream = response.getOutputStream()) {
            String contentType = getContentType(suffix);
            response.setContentType(contentType);
            response.setHeader("Content-disposition", "attachment;filename=" + personTrainingFile.getFileName());
            byte[] bytes = new byte[1024];
            int i;
            while((i = fileInputStream.read(bytes)) != -1) {
                stream.write(bytes, 0, i);
                stream.flush();
            }
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
 
 
 
 
 
 
 
    }
    // 根据文件扩展名获取 MIME 类型
    private String getContentType(String fileExtension) {
        switch (fileExtension) {
            case "jpg":
            case "jpeg":
                return "image/jpeg";
            case "png":
                return "image/png";
            case "gif":
                return "image/gif";
            case "pdf":
                return "application/pdf";
            case "doc":
                return "application/msword";
            case "docx":
                return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            case "xls":
                return "application/vnd.ms-excel";
            case "xlsx":
                return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            case "ppt":
            case "pptx":
                return "application/vnd.ms-powerpoint";
            default:
                return "application/octet-stream"; // 默认二进制流
        }
    }
}