xuhy
2024-08-15 98e7dc8c77c30a6cad499a4a12b5334da1fe16d7
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//package com.ruoyi.common.utils;
//
//import cn.hutool.core.io.unit.DataSize;
//import cn.hutool.core.util.IdUtil;
//import cn.hutool.http.HttpRequest;
//import cn.hutool.http.HttpResponse;
//import com.google.common.collect.ImmutableList;
//import javafx.util.Duration;
//import org.apache.catalina.Manager;
//import org.apache.commons.io.FilenameUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.http.entity.ContentType;
//import org.springframework.mock.web.MockMultipartFile;
//import org.springframework.web.multipart.MultipartFile;
//import javafx.scene.media.Media;
//import javafx.scene.media.MediaPlayer;
//import ws.schild.jave.EncoderException;
//import ws.schild.jave.MultimediaInfo;
//import ws.schild.jave.MultimediaObject;
//
//import  java.io.File;
//
//import java.io.*;
//import java.math.BigDecimal;
//import java.math.RoundingMode;
//import java.net.URL;
//import java.nio.channels.FileChannel;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * MultipartFile和File互转工具类
// */
//public class MultipartFileUtil {
//
//    /**
//     * 输入流转MultipartFile
//     *
//     * @param fileName
//     * @param inputStream
//     * @return
//     */
//    public static MultipartFile getMultipartFile(String fileName, InputStream inputStream) {
//        MultipartFile multipartFile = null;
//        try {
//            multipartFile = new MockMultipartFile(fileName, fileName,
//                    ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return multipartFile;
//    }
//
//    /**
//     * 读取网络文件
//     *
//     * @param url      文件地址
//     * @param fileName 文件名称(需带文件名后缀)
//     * @return
//     */
//    public static MultipartFile getMultipartFile(String url, String fileName) {
//        HttpResponse response = HttpRequest.get(url).execute();
//        InputStream inputStream = response.bodyStream();
//        return MultipartFileUtil.getMultipartFile(fileName, inputStream);
//    }
//
//    /**
//     * File 转MultipartFile
//     *
//     * @param file
//     * @return
//     */
//    public static MultipartFile getMultipartFile(File file) {
//        FileInputStream fileInputStream = null;
//        MultipartFile multipartFile = null;
//        try {
//            fileInputStream = new FileInputStream(file);
//            multipartFile = new MockMultipartFile(file.getName(), file.getName(),
//                    ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return multipartFile;
//    }
//
//    /**
//     * MultipartFileUtil 转File
//     *
//     * @param multipartFile
//     * @return
//     */
//    public static File getFile(MultipartFile multipartFile) {
//        // 获取文件名
//        String fileName = multipartFile.getOriginalFilename();
//        // 获取文件后缀
//        String prefix = "." + getExtensionName(fileName);
//        File file = null;
//        try {
//            // 用uuid作为文件名,防止生成的临时文件重复
//            file = File.createTempFile(IdUtil.simpleUUID(), prefix);
//            // MultipartFile to File
//            multipartFile.transferTo(file);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return file;
//    }
//
//    /**
//     * 获取文件扩展名,不带 .
//     */
//    public static String getExtensionName(String filename) {
//        if ((filename != null) && (filename.length() > 0)) {
//            int dot = filename.lastIndexOf('.');
//            if ((dot > -1) && (dot < (filename.length() - 1))) {
//                return filename.substring(dot + 1);
//            }
//        }
//        return filename;
//    }
//
//    /**
//        前端上传视频之后,根据上传的视频文件获取视频的大小和时长
//        1、获取视频时长
//    */
//    public static int readVideoTime(File source) {
//        int vedioSecond =  Integer.parseInt(parseDuration(source.getAbsolutePath()));
//        return vedioSecond;
//    }
//
//
//    /**
//     * 视频时长
//     *
//     * @param fileUrl
//     * @return String[] 0=秒时长,1=展示时长(格式如 01:00:00)
//     */
//    public static String parseDuration(String fileUrl) {
//        long ls = 0L;
//        String[] length = new String[2];
//        try {
//            //
////            URL source = new URL(fileUrl);
//            // 构造方法 接受URL对象
////            MultimediaObject instance = new MultimediaObject(source);
//            // 构造方法 接受File对象
//            MultimediaObject instance = new MultimediaObject(new File(fileUrl));
//            MultimediaInfo result = instance.getInfo();
//            ls = result.getDuration() / 1000;
//            length[0] = String.valueOf(ls);
//            Integer hour = (int) (ls / 3600);
//            Integer minute = (int) (ls % 3600) / 60;
//            Integer second = (int) (ls - hour * 3600 - minute * 60);
//            String hr = hour.toString();
//            String mi = minute.toString();
//            String se = second.toString();
//            if (hr.length() < 2) {
//                hr = "0" + hr;
//            }
//            if (mi.length() < 2) {
//                mi = "0" + mi;
//            }
//            if (se.length() < 2) {
//                se = "0" + se;
//            }
//
//            String noHour = "00";
//            if (noHour.equals(hr)) {
//                length[1] = mi + ":" + se;
//            } else {
//                length[1] = hr + ":" + mi + ":" + se;
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println(length);//{"20","00:20"}
//        return String.valueOf(ls);
//    }
//
//
//
//
//
//
//    /**
//     * 2、获取文件大小
//     * @param source
//     * @return
//     * //***获取视频大小的时候,由于用到了流,使用完之后一定要及时的关闭流,避免无法删除视频文件***
//     *
//     */
//    public static BigDecimal readFileSize(File source) {
//        //cn.hutool.core.io.unit.DataSize.ofMegabytes()
//        FileChannel fc= null;
//        //String size = "";
//        BigDecimal  size  = null ;
//        try {
//            @SuppressWarnings("resource")
//            FileInputStream fis = new FileInputStream(source);
//            fc= fis.getChannel();
//            BigDecimal fileSize = new BigDecimal(fc.size());
//            //size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";
//            size = fileSize.divide(new BigDecimal(1024*1024), 2, RoundingMode.HALF_UP)  ;
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (null!=fc){
//                try{
//                    fc.close();
//                }catch(IOException e){
//                    e.printStackTrace();
//                }
//            }
//        }
//        return size;
//    }
//
//
//}