危化品全生命周期管理后端
kongzy
2024-08-22 0c73654f55844e34772732914af8cc1e247aab63
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
package com.gkhy.hazmat.system.service.impl;
 
import cn.hutool.core.date.DateUtil;
import com.gkhy.hazmat.common.exception.ApiException;
import com.gkhy.hazmat.system.domain.vo.UploadObjectVO;
import com.gkhy.hazmat.system.service.SysCommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
 
import javax.annotation.Resource;
import java.io.*;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.UUID;
 
@Service
@Slf4j
public class SysCommonServiceImpl implements SysCommonService {
 
    @Value("${image.upload_image}")
    private String uploadPath;
 
 
 
 
    // 使用HashSet存储允许的文件格式,提高查找效率
    private final HashSet<String> FORMATSET = new HashSet<>(Arrays.asList(".mp4",".doc", ".docx", ".ppt", ".pptx", ".pdf"));
 
 
    @Resource(name = "threadPoolTaskExecutor")
    private ThreadPoolTaskExecutor poolTaskExecutor;
 
    @Override
    public UploadObjectVO uploadFile(MultipartFile file) {
        if(file==null){
            throw new ApiException("上传文件不能为空");
        }
        UploadObjectVO uploadObjectVO=doUpload(file);
        return uploadObjectVO;
    }
 
    @Override
    public boolean removeFile(String path) {
        String systemDir=System.getProperty("user.dir");
        String filePath=systemDir+File.separator+path;
        File dirFile=new File(filePath);
        if(!dirFile.exists()){
            throw new ApiException("文件不存在");
        }
        if(!dirFile.isFile()){
            throw new ApiException("非文件,不能删除");
        }
        dirFile.delete();
        return true;
    }
 
 
 
    @Override
    public void removeMinioFile(Long resourceId,String path) {
        String md5="";
        if(path.indexOf("/")!=-1){
            md5=path.substring(0,path.indexOf("/"));
        }else{
            md5=path.substring(0,path.lastIndexOf("."));
        }
 
 
    }
 
    @Override
    public UploadObjectVO doUpload(MultipartFile file){
        String originName=file.getOriginalFilename();
        String filename=originName;
        String subfix=filename.substring(filename.lastIndexOf("."));
        Long size=file.getSize();
        filename= UUID.randomUUID().toString().replace("-","")+subfix;
        String systemDir=System.getProperty("user.dir");
        String dateStr= DateUtil.format(new Date(),"yyyyMMdd");
        String filePath=uploadPath+File.separator+dateStr;
        File dirFile=new File(filePath);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }
        filePath=filePath+File.separator+filename;
        try {
            file.transferTo(new File(systemDir+File.separator+filePath));
        } catch (FileNotFoundException e) {
            throw new ApiException("找不到文件");
        } catch (IOException e) {
            throw new ApiException("发生错误,请联系管理员");
        }
        filePath=filePath.replace("\\","/");
        UploadObjectVO uploadObjectVO=new UploadObjectVO().setFilename(filename)
                .setPath(filePath).setOriginName(originName).setSize(size);
        return uploadObjectVO;
    }
 
    @Override
    public UploadObjectVO doUpload(String imageBase64) {
        String originName="";
        String filename=originName;
        String subfix=".png";
        filename= UUID.randomUUID().toString().replace("-","")+subfix;
        String systemDir=System.getProperty("user.dir");
        String dateStr= DateUtil.format(new Date(),"yyyyMMdd");
        String filePath=uploadPath+File.separator+dateStr;
        File dirFile=new File(filePath);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }
        filePath=filePath+File.separator+filename;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] bytes = decoder.decodeBuffer(imageBase64);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(systemDir+File.separator+filePath);
            out.write(bytes);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            throw new ApiException("找不到文件");
        } catch (IOException e) {
            throw new ApiException("发生错误,请联系管理员");
        }
        filePath=filePath.replace("\\","/");
        UploadObjectVO uploadObjectVO=new UploadObjectVO().setFilename(filename)
                .setPath(filePath).setOriginName(originName);
        return uploadObjectVO;
    }
 
}