|
|
@@ -1,10 +1,7 @@
|
|
|
package com.miekir.shibei.tool.web;
|
|
|
|
|
|
import com.miekir.shibei.tool.TextUtils;
|
|
|
-import sun.net.NetworkClient;
|
|
|
|
|
|
-import javax.imageio.ImageIO;
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
@@ -18,39 +15,47 @@ alias /file/images/eden/manual/;
|
|
|
sudo chmod 777 -R /file/images/
|
|
|
恢复默认权限
|
|
|
sudo chmod 755 /file/images/*/
|
|
|
-public class WebImageTool {
|
|
|
- // todo 客户端使用的时候,记得拼上这个
|
|
|
+public class FileTool {
|
|
|
+ // todo 客户端使用的时候,记得拼上这个(判断不以http开头的才拼)
|
|
|
public static final String BASIC_SERVER_URL = "http://jianjie.life/";
|
|
|
- // 不能小于100KB
|
|
|
- private static final int FILE_SIZE_LIMIT = 100 * 1024;
|
|
|
+
|
|
|
// 只要授权之后,是可以直接写这个路径的,但是由于jar包等冲突(或缺失),会导致war包发布的时候,没有真正运行,也就没有文件写进去了。
|
|
|
- private static final String PATH_AUTO_FOLDER = "/file/images/eden/auto/";
|
|
|
- private static final String CMD_GET_AUTO_FILE_COUNT = "ls -l " + PATH_AUTO_FOLDER + " | grep \"^-\"|wc -l";
|
|
|
- private static final String IMAGE_FORMAT = ".jpg";
|
|
|
- private static final String IMAGE_URL_FORMAT = "auto/%s%s";
|
|
|
+ //private static final String PATH_AUTO_FOLDER = "/file/images/eden/auto/";
|
|
|
+ //private static final String CMD_GET_AUTO_FILE_COUNT = "ls -l " + PATH_AUTO_FOLDER + " | grep \"^-\"|wc -l";
|
|
|
+
|
|
|
+ public static final String FORMAT_IMAGE = ".jpg";
|
|
|
+ public static final String FORMATTER_IMAGE_URL = "auto/%s%s";
|
|
|
+ // 图片不能小于100KB
|
|
|
+ public static final int FILE_IMAGE_SIZE_LIMIT = 100 * 1024;
|
|
|
+
|
|
|
+ public static final String FORMAT_TORRENT = ".torrent";
|
|
|
+ public static final String FORMATTER_TORRENT_URL = "torrent/%s%s";
|
|
|
+
|
|
|
+ public static final String TARGET_DIR_AUTO_IMAGES = "F:\\eden\\images\\auto";
|
|
|
+ public static final String TARGET_DIR_AUTO_TORRENT = "F:\\eden\\torrent\\auto";
|
|
|
|
|
|
- private WebImageTool() {
|
|
|
+ private FileTool() {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 爬虫自动保存图片
|
|
|
*
|
|
|
- * @param imageUrl
|
|
|
+ * @param fileUrl
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String autoSaveImage(String imageUrl) {
|
|
|
- if (TextUtils.isEmpty(imageUrl)) {
|
|
|
+ public static String autoSaveFile(String fileUrl, String defaultFormat, String urlFormatter, String targetDir, int sizeLimit) {
|
|
|
+ if (TextUtils.isEmpty(fileUrl)) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- String format = IMAGE_FORMAT;
|
|
|
- int formatIndex = imageUrl.lastIndexOf(".");
|
|
|
- if (formatIndex != -1 && formatIndex < imageUrl.length() - 1) {
|
|
|
- format = imageUrl.substring(formatIndex);
|
|
|
+ String format = defaultFormat;
|
|
|
+ int formatIndex = fileUrl.lastIndexOf(".");
|
|
|
+ if (formatIndex != -1 && formatIndex < fileUrl.length() - 1) {
|
|
|
+ format = fileUrl.substring(formatIndex);
|
|
|
}
|
|
|
|
|
|
//File folder = new File(PATH_AUTO_FOLDER);
|
|
|
- File folder = new File("F:\\auto");
|
|
|
+ File folder = new File(targetDir);
|
|
|
if (!folder.exists()) {
|
|
|
folder.mkdirs();
|
|
|
}
|
|
|
@@ -74,7 +79,11 @@ public class WebImageTool {
|
|
|
FileOutputStream fileOutputStream = null;
|
|
|
try {
|
|
|
// 为了避免403
|
|
|
- HttpURLConnection connection = ((HttpURLConnection)new URL(imageUrl).openConnection());
|
|
|
+ HttpURLConnection connection = ((HttpURLConnection)new URL(fileUrl).openConnection());
|
|
|
+ // 设置连接主机超时(单位:毫秒)
|
|
|
+ connection.setConnectTimeout(30000);
|
|
|
+ // 设置从主机读取数据超时(单位:毫秒)
|
|
|
+ connection.setReadTimeout(30000);
|
|
|
connection.addRequestProperty("User-Agent", "Mozilla/4.0");
|
|
|
InputStream input;
|
|
|
if (connection.getResponseCode() == 200) {
|
|
|
@@ -92,10 +101,10 @@ public class WebImageTool {
|
|
|
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
|
|
|
fileOutputStream.write(dataBuffer, 0, bytesRead);
|
|
|
}
|
|
|
- if (file.length() < FILE_SIZE_LIMIT) {
|
|
|
+ if (file.length() < sizeLimit) {
|
|
|
return null;
|
|
|
}
|
|
|
- return String.format(IMAGE_URL_FORMAT, String.valueOf(futureFileCount), format);
|
|
|
+ return String.format(urlFormatter, String.valueOf(futureFileCount), format);
|
|
|
} catch (IOException e) {
|
|
|
// handle exception
|
|
|
e.printStackTrace();
|
|
|
@@ -114,8 +123,8 @@ public class WebImageTool {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 去除太小的文件
|
|
|
- if (file.length() < FILE_SIZE_LIMIT) {
|
|
|
+ // 去除太小的文件(必须关闭了流才能删)
|
|
|
+ if (file.length() < sizeLimit) {
|
|
|
file.delete();
|
|
|
}
|
|
|
}
|