PathToolUtil.java
3.64 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
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
package com.ruoyi.utils;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;
public class PathToolUtil {
private static final Logger log = LoggerFactory.getLogger(PathToolUtil.class);
// 维护写入队列 不停的遍历队列内的任务来完成写入 提高指令的响应速度
// private static LinkedList<Runnable> runnableList = new LinkedList<>();
public static boolean generateRelativeImagePath(String catalogue, String imei, String uploadTime, byte[] bits) {
// runnableList.push();
String resourceDirectory = "Bsth-admin/src/main/resources";
String path = StringUtils.format("{}/{}/{}/{}.{}", resourceDirectory,
catalogue, imei, uploadTime, "jpg");
File file = new File(path);
RandomAccessFile raf = null;
if (!file.exists()) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
}
try {
// 创建RandomAccessFile对象,并以读写模式打开文件
raf = new RandomAccessFile(file, "rw");
// 将文件指针移动到文件末尾
raf.seek(file.length());
// 写入数据
raf.write(bits);
} catch (Exception e) {
log.error("图片写入出错:{}", e.getMessage());
// TODO 第三次返回true
} finally {
try {
if (raf != null) {
raf.close();
}
} catch (Exception e) {
log.info(e.getMessage());
}
return true;
}
}
public static boolean generateRelativeSpeechPath(String catalogue, String imei, String uploadTime, byte[] bits) {
// runnableList.push();
String resourceDirectory = "Bsth-admin/src/main/resources";
String path = StringUtils.format("{}/{}/{}/{}.{}", resourceDirectory,
catalogue, imei, uploadTime, "mp3");
File file = new File(path);
RandomAccessFile raf = null;
if (!file.exists()) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
}
try {
// 创建RandomAccessFile对象,并以读写模式打开文件
raf = new RandomAccessFile(file, "rw");
// 将文件指针移动到文件末尾
raf.seek(file.length());
// 写入数据
raf.write(bits);
} catch (Exception e) {
log.error("图片写入出错:{}", e.getMessage());
// TODO 第三次返回true
} finally {
try {
if (raf != null) {
raf.close();
}
} catch (Exception e) {
log.info(e.getMessage());
}
return true;
}
}
public static void main(String[] args) {
try {
int count = 0;
FileInputStream fis = new FileInputStream("C:\\Users\\20412\\Desktop\\Bsth\\岗前检查\\Bsth\\Bsth-admin\\src\\main\\resources\\564.jpg");
byte[] bits = new byte[1024];
int len;
while ((len = fis.read(bits)) != -1) {
count++;
for (int i = 0; i < len; i++) {
System.out.print(bits[i]);
}
System.out.println("分片");
}
System.out.println("共" + count + "片");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}