liujie
2023-05-26 786466f6accfe836160342c56cdf3415e09bcb38
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
package com.stylefeng.guns.modular.system.controller;
 
import com.stylefeng.guns.modular.system.utils.UploadUtil;
import com.stylefeng.guns.modular.system.utils.tips.SuccessTip;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.HashMap;
 
@RestController
@Api(tags = "文件上传")
@RequestMapping("/api/upload")
public class UploadController {
 
    /**
     * 获取列表
     */
    @ApiOperation(value = "上传文件",notes="上传文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
    })
    @PostMapping(value = "/upload")
    @ResponseBody
    public Object upload(MultipartFile multipartFile) {
        File file = MultipartFileToFile(multipartFile);
        String s = UploadUtil.PutObject(multipartFile.getOriginalFilename(), file);
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String contentTypeFor = fileNameMap.getContentTypeFor(s);
        String type=null;
        if(contentTypeFor==null){
            type="video";
        }else {
            type="img";
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("type",type);
        map.put("url",s);
        map.put("deleteUrl",multipartFile.getOriginalFilename());
        return new SuccessTip(map);
    }
 
    @ApiOperation(value = "文件删除",notes="文件删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "用户token(Bearer +token)", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9....."),
            @ApiImplicitParam(name = "url", value = "url", required = true, dataType = "String"),
    })
    @GetMapping(value = "/fileDelete")
    @ResponseBody
    public Object fileDelete(@RequestParam String url) {
         UploadUtil.DeleteObject(url);
         return new SuccessTip();
    }
 
 
    public static File MultipartFileToFile(MultipartFile multiFile) {
        // 获取文件名
        String fileName = multiFile.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 若需要防止生成的临时文件重复,可以在文件名后添加随机码
 
        try {
            File file = File.createTempFile(fileName, prefix);
            multiFile.transferTo(file);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}