-
Notifications
You must be signed in to change notification settings - Fork 36
/
FileTools.java
245 lines (218 loc) · 6.97 KB
/
FileTools.java
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package com.backend;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.imageio.ImageIO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.utils.conf.AppConfig;
public class FileTools
{
private static final Logger logger = LoggerFactory
.getLogger(FileTools.class);
public static boolean usesqlite = "true"
.equals(System.getProperty("usesqlite", "true")) ? true : false;
public static long lastScanTime = 0;
public static final ExecutorService threadPool = Executors
.newFixedThreadPool(AppConfig.getInstance().getThreadCount());
public static void readShortFileContent(byte[] buffer, File f)
throws FileNotFoundException, IOException
{
int maxLen = buffer.length;
FileInputStream fin = null;
try
{
fin = new FileInputStream(f);
int offset = 0;
while (true)
{
int readlen = fin.read(buffer, offset, maxLen - offset);
if (readlen < 0)
{
break;
}
offset += readlen;
if (offset >= maxLen)
{
break;
}
}
}
finally
{
if (fin != null)
{
fin.close();
}
}
}
public static boolean checkFileDeleted(final FileInfo fi,
List<String> excludeDirs) throws IOException
{
if (fi != null)
{
File f = new File(fi.getPath());
if (f.isFile()
&& f.length() > AppConfig.getInstance().getMinFileSize())
{
if (fi.getcTime().getTime() == FileTools
.getFileCreateTime(new File(fi.getPath())))
{
if (excludeDirs != null)
{
for (String dir : excludeDirs)
{
if (fi.getPath().startsWith(dir))
{
return true;
}
}
}
return false;
}
/*
* String tmpstr = fi.getPath().toLowerCase(); if
* (tmpstr.endsWith("jpg") || tmpstr.endsWith("jpeg")) { final
* int angel = ReadEXIF.needRotateAngel(fi.getPath()); if (angel
* != 0) { threadPool.submit(new Runnable() {
*
* @Override public void run() { rotateOneFile(fi, angel); } });
* return true; } }
*/
}
}
return true;
}
@SuppressWarnings("unused")
private static void rotateOneFile(FileInfo fi, int angel)
{
FileTools.rotatePhonePhoto(fi.getPath(), angel);
SimpleDateFormat sf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
try
{
String tmpFilePath = fi.getPath() + "_tmpfile";
new File(fi.getPath()).renameTo(new File(tmpFilePath));
ExifCreator.addExifDate(new String[] { tmpFilePath, fi.getPath(),
sf.format(fi.getPhotoTime()) });
}
catch (Exception e)
{
logger.error("caught: ", e);
}
}
public static long getFileCreateTime(File f) throws IOException
{
if (f == null)
{
return -1;
}
Path path = FileSystems.getDefault().getPath(f.getParent(),
f.getName());
BasicFileAttributes attrs = Files.readAttributes(path,
BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
return attrs.creationTime().toMillis();
}
/**
* 旋转照片
*
* @return
*/
public static void rotatePhonePhoto(String fullPath, int angel)
{
BufferedImage src;
try
{
File oldFile = new File(fullPath);
src = ImageIO.read(oldFile);
int src_width = src.getWidth(null);
int src_height = src.getHeight(null);
int swidth = src_width;
int sheight = src_height;
if (angel == 90 || angel == 270)
{
swidth = src_height;
sheight = src_width;
}
Rectangle rect_des = new Rectangle(new Dimension(swidth, sheight));
BufferedImage res = new BufferedImage(rect_des.width,
rect_des.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = res.createGraphics();
g2.translate((rect_des.width - src_width) / 2,
(rect_des.height - src_height) / 2);
g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);
g2.drawImage(src, null, null);
File newTmpFile = new File(fullPath + "tmp_bak");
ImageIO.write(res, "jpg", newTmpFile);
oldFile.renameTo(new File(fullPath + ".old"));
newTmpFile.renameTo(oldFile);
}
catch (IOException e)
{
logger.warn("cause by: ", e);
}
}
public static boolean copyFile(String src, String dst)
{
BufferedInputStream fis = null;
BufferedOutputStream fos = null;
try
{
byte[] iobuff = new byte[1024 * 16];
fis = new BufferedInputStream(new FileInputStream(src));
fos = new BufferedOutputStream(new FileOutputStream(dst));
int bytes = -1;
while ((bytes = fis.read(iobuff)) != -1)
{
fos.write(iobuff, 0, bytes);
}
return true;
}
catch (Exception e)
{
logger.warn("copy file failed!", e);
}
finally
{
if (fis != null)
{
try
{
fis.close();
}
catch (IOException e)
{
logger.warn("caused by: ", e);
}
}
if (fos != null)
{
try
{
fos.close();
}
catch (IOException e)
{
logger.warn("caused by: ", e);
}
}
}
return false;
}
}