xiaoyong931011
2021-12-22 f0afca8e5611e46557f69cd22850c630388d329f
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
package cc.mrbird.febs.video.controller;
 
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.utils.FileUtil;
import cn.hutool.core.lang.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;
 
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * @author wzy
 * @date 2021-12-14
 **/
@Slf4j
@RequestMapping(value = "/common")
@RestController
@RequiredArgsConstructor
public class CommonController {
 
    @Value("${system.images.path}")
    private String baseSavePath;
 
    @Value("${system.images.url}")
    private String baseUrl;
 
 
 
    @RequestMapping(value = "/upload")
    public FebsResponse upload(MultipartRequest request) throws IOException {
        // 保存路径 /Users/helius/Desktop/
//        baseSavePath = "/Users/helius/Desktop/";
 
        // 访问路径
//        baseUrl = "http://localhost:1234/";
        List<String> visitPathes = updateImg(request);
        return new FebsResponse().success().data(visitPathes);
    }
 
    private List<String> updateImg(MultipartRequest request) {
        // 检查目录
        File uploadDir = new File(baseSavePath);
        if (!uploadDir.isDirectory()) {
            uploadDir.mkdir();
        }
 
        Map<String, MultipartFile> fileMaps = request.getFileMap();
        List<String> visitPathes = new ArrayList<>();
        for (String key : fileMaps.keySet()) {
            MultipartFile file = fileMaps.get(key);
 
            // 拼接64位文件名
            String fileName = file.getOriginalFilename();
            String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
            String newFileName = UUID.randomUUID().toString() + "." + fileExt;
            Map<String, String> fileUrlMap = fileUrl(baseSavePath, baseUrl);
            String savePath = fileUrlMap.get("savePath");
            String saveUrl = fileUrlMap.get("saveUrl");
            File uploadedFile = new File(savePath, newFileName);
            try {
                FileCopyUtils.copy(file.getBytes(), uploadedFile);
            } catch (IOException e) {
                throw new FebsException("上传失败");
            }
            // 图片访问地址
            String visitPath = saveUrl + newFileName;
            visitPathes.add(visitPath);
 
        }
 
        return visitPathes;
    }
 
    private Map<String, String> fileUrl(String savePath, String saveUrl) {
        Map<String, String> fileUrlMap = new HashMap<>();
 
        // 创建日期文件夹
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String ymd = sdf.format(new Date());
        savePath += ymd + File.separatorChar;
        saveUrl += ymd + File.separatorChar;
        File dirFile = new File(savePath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
 
        fileUrlMap.put("savePath", savePath);
        fileUrlMap.put("saveUrl", saveUrl);
        return fileUrlMap;
 
    }
 
    public static void main(String[] args) throws EncoderException {
        File file = new File("http://120.27.238.55:8000/video/20211216/9c7d8952-aadb-4fa1-b9c7-a07ab414a394.mp4");
 
        boolean directory = file.isDirectory();
        boolean video = FileUtil.isVideo(file);
        MultimediaObject multimediaObject = new MultimediaObject(file);
        MultimediaInfo info = multimediaObject.getInfo();
        int duration = (int) Math.ceil((double) info.getDuration() / 1000);
        System.out.println(duration);
    }
}