whyczh
2022-05-17 fa40fbeb5a288d32f8f14e7a40069b2bb38053d7
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
package com.whyc.service;
 
import com.whyc.dto.Response;
import com.whyc.dto.paramter.UserWorkAlarmParam;
import com.whyc.mapper.UserWorkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Service
public class UserWorkService {
    @Resource
    private UserWorkMapper mapper;
    @Autowired
    private Environment environment;
 
 
 
    public Response uploadAlarmFile(MultipartFile[] file, UserWorkAlarmParam param) {
        String fileDirName = "";
        int configType = Integer.parseInt(environment.getProperty("configFile.type"));
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        File jarFile = applicationHome.getDir();
        //测试版
        if(configType==1){
            fileDirName = jarFile.getParentFile().toString();
        }else{
            //打包版
            fileDirName = jarFile.toString();
        }
        String root=fileDirName+"/stationsrc/alarm"+ param.getStationId() + "/" + param.getAfterOrBefore() + "/";
        List<String> filePathList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        boolean isSuccess = false;
        try {
            for (int i = 0; i < file.length && param != null; i++) {
                String fileFileName = file[i].getOriginalFilename();
                fileFileName= fileFileName.replace(".","_"+sdf.format(new Date())+".");
                String filePath = root + fileFileName;
                createFilefolderIFNotExist(filePath);
                file[i].transferTo(new File(filePath));
                isSuccess = true;
                filePathList.add(filePath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (isSuccess) {
            return new Response().set(1,filePathList,"上传成功");
        } else {
            return new Response().set(0,false,"上传失败");
        }
 
    }
    //将文件复制到指定的目录
    public void copyFile(File oldFile, String newPath){
        File file = new File(newPath);
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(oldFile);
            out = new FileOutputStream(file);;
 
            byte[] buffer=new byte[2097152];
            int readByte = 0;
            while((readByte = in.read(buffer)) != -1){
                out.write(buffer, 0, readByte);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public void createFilefolderIFNotExist(String filePath){
        File f = new File(filePath);
        if(!f.exists()){
            if(!f.getParentFile().exists()){
                f.getParentFile().mkdirs();
            }
        }
    }
 
 
}