VideoUtil.java
3.1 KB
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
package com.viontech.util;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.web.multipart.MultipartFile;
public class VideoUtil {
public static SimpleDateFormat FORMAT_YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");
public static String saveVideo(MultipartFile videofile, String videoPath, String videoName) throws IOException {
if (videofile == null || videofile.isEmpty())
return null;
videoPath = videoPath + "/" + FORMAT_YYYY_MM_DD.format(new Date());
File videoFile = null;
String fileName = videofile.getOriginalFilename();
String videoFilePath = videoPath + "/" + videoName;
makerDir(videoPath);
System.out.println("videoFilePath=" + videoFilePath);
InputStream is = videofile.getInputStream();
videoFile = new File(videoFilePath);
videofile.transferTo(videoFile);
return videoFilePath;
}
public static String save(InputStream videofile, String videoPath, String videoName) {
if (videofile == null) {
System.out.println("videofile == null");
return null;
}
videoPath = videoPath + "/" + FORMAT_YYYY_MM_DD.format(new Date());
String videoFilePath = videoPath + "/" + videoName;
makerDir(videoPath);
FileOutputStream fos = null;
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
fos = new FileOutputStream(videoFilePath);
FileInputStream fis = (FileInputStream)videofile;
inChannel = fis.getChannel();
outChannel = fos.getChannel();
inChannel.transferTo(0L, inChannel.size(), outChannel);
inChannel.close();
outChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
return videoFilePath;
}
public static byte[] getBety(String filePath) throws IOException {
File file = new File(filePath);
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
inputFile.read(buffer);
inputFile.close();
return buffer;
}
public static InputStream getInputStream(String filePath) throws IOException {
InputStream inputStream = new ByteArrayInputStream(getBety(filePath));
return inputStream;
}
public static boolean makerDir(String filePath) {
try {
File dirFile = new File(filePath);
if (!dirFile.exists()) {
dirFile.setWritable(true, false);
dirFile.mkdirs();
}
File fp = new File(filePath);
dirFile = null;
if (fp.exists()) {
fp = null;
return true;
}
fp = null;
return false;
} catch (Exception e) {
return false;
}
}
}