Pu Zhibing
3 天以前 3244b550596e0330031b3f4547356927df83b0ad
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.ruoyi.account.controller;
 
import com.alibaba.fastjson2.util.UUIDUtils;
import com.ruoyi.account.config.FileUploadConfig;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
 
/**
 * 文件上传控制类
 *
 * @author junelee
 * @date 2020/3/20 20:21
 */
@Api(tags = "服务器文件上传")
@RestController
@RequestMapping("/file/")
public class FileController {
 
    @Autowired
    private FileUploadConfig fileUploadConfig;
    
    @Resource
    private TokenService tokenService;
    
    @Resource
    private RedisTemplate redisTemplate;
 
 
 
    @ApiOperation(value = "单文件上传", notes = "单文件上传,rename 默认不重命名")
    @PostMapping(value = "upload", headers = "content-type=multipart/form-data")
    public AjaxResult uploadImageMany(@RequestParam(value = "file") MultipartFile mf) throws IOException {
        Long userId = tokenService.getLoginUserApplet().getUserId();
        if(null == userId){
            return AjaxResult.error("请先登录");
        }
        if (mf.isEmpty()) {
            return AjaxResult.error("请传入文件!");
        }
        String TimeDir =new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String realPath = fileUploadConfig.getLocation() + TimeDir;
        File file = new File(realPath);
        // 没有目录就创建
        if (!file.exists()) {
            file.mkdirs();
        }
        // 获取文件名称
        String filename = mf.getOriginalFilename();
        if(filename.contains("../")){
            filename = filename.replaceAll("\\.\\./", "");
        }
        // 获取文件后缀
        String ext = filename.substring(filename.lastIndexOf(".") + 1);
        // 检查文件类型
        if (!fileUploadConfig.getAllowExt().contains(ext)) {
            return AjaxResult.error("上传文件格式不正确,仅支持" + fileUploadConfig.getAllowExt());
        }
        filename = UUID.randomUUID() + "." + ext;
        File targetFile = new File(realPath, filename);//目标文件
        //开始从源文件拷贝到目标文件
        //传图片一步到位
        mf.transferTo(targetFile);
        //拼接数据
        String imgstr = fileUploadConfig.getAccessPath() + TimeDir +"/"+ filename;
        redisTemplate.opsForValue().set("file:" + userId, filename, 1, TimeUnit.HOURS);
        return AjaxResult.success(imgstr);
    }
    @ApiOperation(value = "单文件上传", notes = "单文件上传,rename 默认不重命名")
    @PostMapping(value = "uploadManage", headers = "content-type=multipart/form-data")
    public AjaxResult uploadManage(@RequestParam(value = "file") MultipartFile mf) throws IOException {
        Long userId = tokenService.getLoginUser().getUserid();
        if(null == userId){
            return AjaxResult.error("请先登录");
        }
        if (mf.isEmpty()) {
            return AjaxResult.error("请传入文件!");
        }
        String TimeDir =new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String realPath = fileUploadConfig.getLocation() + TimeDir;
        File file = new File(realPath);
        // 没有目录就创建
        if (!file.exists()) {
            file.mkdirs();
        }
        // 获取文件名称
        String filename = mf.getOriginalFilename();
        if(filename.contains("../")){
            filename = filename.replaceAll("\\.\\./", "");
        }
        // 获取文件后缀
        String ext = filename.substring(filename.lastIndexOf(".") + 1);
        // 检查文件类型
        if (!fileUploadConfig.getAllowExt().contains(ext)) {
            return AjaxResult.error("上传文件格式不正确,仅支持" + fileUploadConfig.getAllowExt());
        }
        filename = UUID.randomUUID() + "." + ext;
        File targetFile = new File(realPath, filename);//目标文件
        //开始从源文件拷贝到目标文件
        //传图片一步到位
        mf.transferTo(targetFile);
        //拼接数据
        String imgstr = fileUploadConfig.getAccessPath() + TimeDir +"/"+ filename;
        redisTemplate.opsForValue().set("file:" + userId, filename, 1, TimeUnit.HOURS);
        return AjaxResult.success(imgstr);
    }
 
 
//    @ApiOperation(value = "单文件上传", notes = "单文件上传,rename 默认不重命名")
//    @PostMapping(value = "strUpload", headers = "content-type=multipart/form-data")
//    public String strUpload(@RequestParam(value = "file") MultipartFile mf,@RequestParam(value = "fileName")String fileName) throws IOException {
//        if (mf.isEmpty()) {
//            return "请传入文件!";
//        }
//        String TimeDir =new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//        String realPath = fileUploadConfig.getQrLocation() + TimeDir;
//        File file = new File(realPath);
//        // 没有目录就创建
//        if (!file.exists()) {
//            file.mkdirs();
//        }
//        File targetFile = new File(realPath, fileName);//目标文件
//        //开始从源文件拷贝到目标文件
//        //传图片一步到位
//        mf.transferTo(targetFile);
//        //拼接数据
//        return fileUploadConfig.getQrLocation() + TimeDir +"\\"+ fileName;
//    }
 
    @ApiOperation(value = "单文件上传", notes = "单文件上传,rename 默认不重命名")
    @PostMapping(value = "strUpload", headers = "content-type=multipart/form-data")
    public String strUpload(@RequestParam(value = "file") MultipartFile mf,@RequestParam(value = "fileName")String fileName) throws IOException {
        if (mf.isEmpty()) {
            return "请传入文件!";
        }
        String TimeDir =new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//        String realPath = "D:\\file\\" + TimeDir;
        String realPath = fileUploadConfig.getLocation() + TimeDir;
        File file = new File(realPath);
        // 没有目录就创建
        if (!file.exists()) {
            file.mkdirs();
        }
        File targetFile = new File(realPath, fileName);//目标文件
        //开始从源文件拷贝到目标文件
        //传图片一步到位
        mf.transferTo(targetFile);
        String imgstr = fileUploadConfig.getAccessPath() + TimeDir +"/"+ fileName;
        //拼接数据
//        return TimeDir +"/"+ fileName;
        return imgstr;
    }
 
    @ApiOperation(value = "单文件上传(覆盖服务器原文件)", notes = "单文件上传,rename 默认不重命名")
    @PostMapping(value = "test/upload", headers = "content-type=multipart/form-data")
    public AjaxResult uploadTest(@RequestParam(value = "file") MultipartFile mf) throws IOException {
        if (mf.isEmpty()) {
            return AjaxResult.error("请传入文件!");
        }
        String TimeDir = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String realPath = fileUploadConfig.getLocation() + "2021-11-17";
        File file = new File(realPath);
        // 没有目录就创建
        if (!file.exists()) {
            file.mkdirs();
        }
        // 判断文件大小
        String filename = "6u2mGlqHkeE75e2ab51b4a03c6982ff7d68f4c024d43.jpg";
        // 获取文件后缀
        String ext = filename.substring(filename.lastIndexOf("."), filename.length());
        // 检查文件类型
        if (!fileUploadConfig.getAllowExt().contains(ext)) {
            return AjaxResult.error("上传文件格式不正确,仅支持" + fileUploadConfig.getAllowExt());
        }
        File targetFile = new File(realPath, filename);//目标文件
        //开始从源文件拷贝到目标文件
        //传图片一步到位
        mf.transferTo(targetFile);
        //拼接数据
        String imgstr = fileUploadConfig.getAccessPath() + "2021-11-17" + "/" + filename;
        return AjaxResult.success(imgstr);
    }
 
 
}