kongzy
2023-11-27 59ae9759fd50568059fa44f43832f5a414edb3e9
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
package com.gkhy.assess.system.service.impl;
 
import com.gkhy.assess.common.exception.ApiException;
import com.gkhy.assess.system.domain.vo.UploadObjectVO;
import com.gkhy.assess.system.service.SysCommonService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;
 
@Service
public class SysCommonServiceImpl implements SysCommonService {
 
    @Value("${image.upload_file}")
    private String uploadFilePath;
 
    @Value("${image.upload_image}")
    private String uploadImagePath;
 
    @Override
    public UploadObjectVO uploadFile(MultipartFile file) {
        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.isFile()){
            throw new ApiException("非文件,不能删除");
        }
        if(!dirFile.exists()){
            throw new ApiException("文件不存在");
        }
        dirFile.delete();
        return true;
    }
 
    public UploadObjectVO doUpload(MultipartFile file){
        String filename=file.getOriginalFilename();
        String subfix=filename.substring(filename.lastIndexOf("."));
        filename= UUID.randomUUID().toString().replace("-","")+subfix;
        String systemDir=System.getProperty("user.dir");
        String filePath="";
        if(checkImageType(subfix)){
            filePath=uploadImagePath;
        }else{
            filePath=uploadFilePath;
        }
        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("发生错误,请联系管理员");
        }
        UploadObjectVO uploadObjectVO=new UploadObjectVO().setFilename(filename)
                .setPath(filePath);
        return uploadObjectVO;
    }
 
 
    public boolean checkImageType(String subfix){
        if(".jpg".equalsIgnoreCase(subfix)||
                ".jpeg".equalsIgnoreCase(subfix)||
                ".png".equalsIgnoreCase(subfix)||
                ".bmp".equalsIgnoreCase(subfix)||
                ".tif".equalsIgnoreCase(subfix)
        ){
            return true;
        }
        return false;
    }
 
}