liding
2026-08-28 685257c3dc80fb732480480128269cefa51e50e5
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
package com.ruoyi.inspect.service.impl;
 
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 java.io.File;
 
/**
* @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());
        }
    }
 
}