xuefei
2020-12-10 eeeb7233935ea9b10e99043bdbf740ef86c9bf20
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
package cn.cetc54.platform.base.manager.impl;
 
 
import cn.cetc54.platform.base.manager.FileManage;
import cn.cetc54.platform.core.common.exception.PlatformException;
import cn.cetc54.platform.core.config.properties.OSSProperties;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.*;
 
/**
 * @author x
 */
@Slf4j
@Component
public class LocalFileManage implements FileManage {
 
    @Autowired
    private OSSProperties ossProperties;
 
 
 
    @Override
    @Deprecated
    public String pathUpload(String filePath, String key) {
 
        throw new PlatformException("暂不支持");
    }
 
    /**
     * 实则为路径上传
     * @param inputStream
     * @param key
     * @param file
     * @return
     */
    @Override
    public String inputStreamUpload(InputStream inputStream, String key, MultipartFile file) {
 
        String day = DateUtil.format(DateUtil.date(), "yyyyMMdd");
        String path = ossProperties.getFilePath() + "/" + day;
        File dir = new File(path);
        if(!dir.exists()){
            dir.mkdirs();
        }
        File f = new File(path + "/" + key);
        if(f.exists()){
            throw new PlatformException("文件名已存在");
        }
        try {
            file.transferTo(f);
            return path + "/" + key;
        } catch (IOException e) {
            log.error(e.toString());
            throw new PlatformException("上传文件出错");
        }
    }
 
    /**
     * 注意此处需传入url
     * @param url
     * @param toKey
     * @return
     */
    @Override
    public String renameFile(String url, String toKey) {
 
        String result = copyFile(url, toKey);
        deleteFile(url);
        return result;
    }
 
    /**
     * 注意此处需传入url
     * @param url
     * @param toKey
     * @return
     */
    @Override
    public String copyFile(String url, String toKey) {
 
        File file = new File(url);
        FileInputStream i = null;
        FileOutputStream o = null;
 
        try {
            i = new FileInputStream(file);
            o = new FileOutputStream(new File(file.getParentFile() + "/" + toKey));
 
            byte[] buf = new byte[1024];
            int bytesRead;
 
            while ((bytesRead = i.read(buf))>0){
                o.write(buf, 0, bytesRead);
            }
 
            i.close();
            o.close();
            return file.getParentFile() + "/" + toKey;
        } catch (IOException e) {
            log.error(e.toString());
            throw new PlatformException("复制文件出错");
        }
    }
 
    /**
     * 注意此处需传入url
     * @param url
     */
    @Override
    public void deleteFile(String url) {
 
        File file = new File(url);
        if (file.exists()) {
            file.delete();
        }
    }
 
    /**
     * 读取文件
     * @param url
     * @param response
     */
    public static void view(String url, HttpServletResponse response){
 
        File file = new File(url);
        FileInputStream in = null;
        OutputStream out = null;
 
        try {
            if (file.exists()) {
                in = new FileInputStream(file);
                out = response.getOutputStream();
 
                byte[] buf = new byte[1024];
                int bytesRead;
 
                while ((bytesRead = in.read(buf)) > 0) {
                    out.write(buf, 0, bytesRead);
                    out.flush();
                }
            }
        } catch (IOException e) {
 
        } finally {
            if(out!=null){
                try {
                    in.close();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}