更新
This commit is contained in:
@@ -4,6 +4,7 @@ import com.google.common.collect.Maps;
|
||||
import com.google.common.io.Files;
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.bean.Property;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import com.ilummc.tlib.util.Ref;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.MemoryConfiguration;
|
||||
@@ -179,9 +180,7 @@ public class ConfigUtils {
|
||||
configuration.loadFromString(yaml);
|
||||
return configuration;
|
||||
} catch (Exception e) {
|
||||
TLib.getTLib().getLogger().error("配置文件载入失败!");
|
||||
TLib.getTLib().getLogger().error("插件: &4" + plugin.getName());
|
||||
TLib.getTLib().getLogger().error("文件: &4" + file);
|
||||
TLocale.Logger.error("FILE-UTILS.FALL-LOAD-CONFIGURATION", plugin.getName(), file.getName());
|
||||
}
|
||||
return configuration;
|
||||
}
|
||||
|
||||
@@ -2,51 +2,58 @@ package me.skymc.taboolib.fileutils;
|
||||
|
||||
import ch.njol.util.Closeable;
|
||||
import me.skymc.taboolib.message.MsgUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.util.Objects;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
public static String ip() {
|
||||
URL url;
|
||||
URLConnection con;
|
||||
try {
|
||||
url = new URL("http://1212.ip138.com/ic.asp");
|
||||
con = url.openConnection();
|
||||
} catch (Exception ignored) {
|
||||
return "[IP ERROR]";
|
||||
}
|
||||
InputStream ins = null;
|
||||
InputStreamReader inputStreamReader = null;
|
||||
BufferedReader bufferedReader = null;
|
||||
try {
|
||||
InputStream ins = null;
|
||||
URL url = new URL("http://1212.ip138.com/ic.asp");
|
||||
URLConnection con = url.openConnection();
|
||||
ins = con.getInputStream();
|
||||
InputStreamReader isReader = new InputStreamReader(ins, "GB2312");
|
||||
BufferedReader bReader = new BufferedReader(isReader);
|
||||
inputStreamReader = new InputStreamReader(ins, "GB2312");
|
||||
bufferedReader = new BufferedReader(inputStreamReader);
|
||||
StringBuilder webContent = new StringBuilder();
|
||||
String str = null;
|
||||
while ((str = bReader.readLine()) != null) {
|
||||
webContent.append(str);
|
||||
}
|
||||
bufferedReader.lines().forEach(webContent::append);
|
||||
int start = webContent.indexOf("[") + 1;
|
||||
int end = webContent.indexOf("]");
|
||||
ins.close();
|
||||
return webContent.substring(start, end);
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
} catch (Exception ignored) {
|
||||
return "[IP ERROR]";
|
||||
} finally {
|
||||
IOUtils.close(con);
|
||||
IOUtils.closeQuietly(bufferedReader);
|
||||
IOUtils.closeQuietly(inputStreamReader);
|
||||
IOUtils.closeQuietly(ins);
|
||||
}
|
||||
return "[IP ERROR]";
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并获取文件
|
||||
* 检测文件并创建
|
||||
*
|
||||
* @param filePath
|
||||
* @return
|
||||
* @param file 文件
|
||||
*/
|
||||
public static File file(String filePath) {
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
public static File createNewFile(File file) {
|
||||
if (file != null && !file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
return file;
|
||||
@@ -55,26 +62,28 @@ public class FileUtils {
|
||||
/**
|
||||
* 创建并获取文件
|
||||
*
|
||||
* @param Path
|
||||
* @param filePath
|
||||
* @param Path 目录
|
||||
* @param filePath 地址
|
||||
* @return
|
||||
*/
|
||||
public static File file(File Path, String filePath) {
|
||||
File file = new File(Path, filePath);
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
}
|
||||
return file;
|
||||
return createNewFile(new File(Path, filePath));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并获取文件
|
||||
*
|
||||
* @param filePath 地址
|
||||
* @return {@link File}
|
||||
*/
|
||||
public static File file(String filePath) {
|
||||
return createNewFile(new File(filePath));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件夹
|
||||
*
|
||||
* @param file
|
||||
* @param file 文件夹
|
||||
*/
|
||||
public void deleteAllFile(File file) {
|
||||
if (!file.exists()) {
|
||||
@@ -84,8 +93,7 @@ public class FileUtils {
|
||||
file.delete();
|
||||
return;
|
||||
}
|
||||
File[] files = file.listFiles();
|
||||
for (File file1 : files) {
|
||||
for (File file1 : Objects.requireNonNull(file.listFiles())) {
|
||||
deleteAllFile(file1);
|
||||
}
|
||||
file.delete();
|
||||
@@ -96,20 +104,19 @@ public class FileUtils {
|
||||
*
|
||||
* @param file1 文件1
|
||||
* @param file2 文件2
|
||||
* @throws Exception
|
||||
*/
|
||||
public void copyAllFile(String file1, String file2) throws Exception {
|
||||
public void copyAllFile(String file1, String file2) {
|
||||
File _file1 = new File(file1);
|
||||
File _file2 = new File(file2);
|
||||
if (!_file2.exists()) {
|
||||
if (!_file1.isDirectory()) {
|
||||
_file2.createNewFile();
|
||||
createNewFile(_file2);
|
||||
} else {
|
||||
_file2.mkdirs();
|
||||
}
|
||||
}
|
||||
if (_file1.isDirectory()) {
|
||||
for (File file : _file1.listFiles()) {
|
||||
for (File file : Objects.requireNonNull(_file1.listFiles())) {
|
||||
if (file.isDirectory()) {
|
||||
copyAllFile(file.getAbsolutePath(), file2 + "/" + file.getName());
|
||||
} else {
|
||||
@@ -138,43 +145,33 @@ public class FileUtils {
|
||||
channelIn = fileIn.getChannel();
|
||||
channelOut = fileOut.getChannel();
|
||||
channelIn.transferTo(0, channelIn.size(), channelOut);
|
||||
} catch (Exception e) {
|
||||
//
|
||||
} catch (IOException ignored) {
|
||||
} finally {
|
||||
try {
|
||||
fileIn.close();
|
||||
channelIn.close();
|
||||
fileOut.close();
|
||||
channelOut.close();
|
||||
} catch (Exception e) {
|
||||
//
|
||||
}
|
||||
IOUtils.closeQuietly(channelIn);
|
||||
IOUtils.closeQuietly(channelOut);
|
||||
IOUtils.closeQuietly(fileIn);
|
||||
IOUtils.closeQuietly(fileOut);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过输入流读取文本
|
||||
*
|
||||
* @param in
|
||||
* @param size
|
||||
* @param encode
|
||||
* @return
|
||||
* @param in 输入流
|
||||
* @param size 大小
|
||||
* @param encode 编码
|
||||
* @return 文本
|
||||
*/
|
||||
public static String getStringFromInputStream(InputStream in, int size, String encode) {
|
||||
try {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
|
||||
byte[] b = new byte[size];
|
||||
int i = 0;
|
||||
|
||||
int i;
|
||||
while ((i = in.read(b)) > 0) {
|
||||
bos.write(b, 0, i);
|
||||
}
|
||||
|
||||
bos.close();
|
||||
return new String(bos.toByteArray(), encode);
|
||||
} catch (IOException e) {
|
||||
MsgUtils.warn("输入流读取出错: &4" + e.getMessage());
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -182,30 +179,28 @@ public class FileUtils {
|
||||
/**
|
||||
* 通过文件读取文本
|
||||
*
|
||||
* @param file
|
||||
* @param size
|
||||
* @param encode
|
||||
* @return
|
||||
* @param file 文件
|
||||
* @param size 大小
|
||||
* @param encode 编码
|
||||
* @return 文本
|
||||
*/
|
||||
public static String getStringFromFile(File file, int size, String encode) {
|
||||
FileInputStream fin = null;
|
||||
BufferedInputStream bin = null;
|
||||
try {
|
||||
FileInputStream fin = new FileInputStream(file);
|
||||
BufferedInputStream bin = new BufferedInputStream(fin);
|
||||
fin = new FileInputStream(file);
|
||||
bin = new BufferedInputStream(fin);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
|
||||
byte[] b = new byte[size];
|
||||
int i = 0;
|
||||
|
||||
int i;
|
||||
while ((i = bin.read(b)) > 0) {
|
||||
bos.write(b, 0, i);
|
||||
}
|
||||
|
||||
bos.close();
|
||||
bin.close();
|
||||
fin.close();
|
||||
return new String(bos.toByteArray(), encode);
|
||||
} catch (IOException e) {
|
||||
MsgUtils.warn("文件读取出错: &4" + e.getMessage());
|
||||
} catch (IOException ignored) {
|
||||
} finally {
|
||||
IOUtils.closeQuietly(bin);
|
||||
IOUtils.closeQuietly(fin);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -213,82 +208,80 @@ public class FileUtils {
|
||||
/**
|
||||
* 通过 URL 读取文本
|
||||
*
|
||||
* @param url
|
||||
* @param size
|
||||
* @return
|
||||
* @param url 地址
|
||||
* @param def 默认值
|
||||
* @return 文本
|
||||
*/
|
||||
public static String getStringFromURL(String url, int size) {
|
||||
try {
|
||||
URLConnection conn = new URL(url).openConnection();
|
||||
BufferedInputStream bin = new BufferedInputStream(conn.getInputStream());
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
|
||||
byte[] b = new byte[size];
|
||||
int i = 0;
|
||||
|
||||
while ((i = bin.read(b)) > 0) {
|
||||
bos.write(b, 0, i);
|
||||
}
|
||||
|
||||
bos.close();
|
||||
bin.close();
|
||||
return new String(bos.toByteArray(), conn.getContentEncoding() == null ? "UTF-8" : conn.getContentEncoding());
|
||||
} catch (IOException e) {
|
||||
MsgUtils.warn("网络访问出错: &4" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getStringFromURL(String url, String def) {
|
||||
String s = getStringFromURL(url, 1024);
|
||||
return s == null ? def : s;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 URL 读取文本
|
||||
*
|
||||
* @param url 地址
|
||||
* @param size 大小
|
||||
* @return 文本
|
||||
*/
|
||||
public static String getStringFromURL(String url, int size) {
|
||||
URLConnection conn = null;
|
||||
BufferedInputStream bin = null;
|
||||
try {
|
||||
conn = new URL(url).openConnection();
|
||||
bin = new BufferedInputStream(conn.getInputStream());
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
byte[] b = new byte[size];
|
||||
int i;
|
||||
while ((i = bin.read(b)) > 0) {
|
||||
bos.write(b, 0, i);
|
||||
}
|
||||
return new String(bos.toByteArray(), conn.getContentEncoding() == null ? "UTF-8" : conn.getContentEncoding());
|
||||
} catch (IOException ignored) {
|
||||
} finally {
|
||||
IOUtils.close(conn);
|
||||
IOUtils.closeQuietly(bin);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载文件
|
||||
*
|
||||
* @param urlStr
|
||||
* @param filename
|
||||
* @param saveDir
|
||||
* @param downloadURL 下载地址
|
||||
* @param file 保存位置
|
||||
*/
|
||||
public static void download(String urlStr, String filename, File saveDir) {
|
||||
public static void download(String downloadURL, File file) {
|
||||
HttpURLConnection conn = null;
|
||||
InputStream inputStream = null;
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
|
||||
// 超时时间
|
||||
URL url = new URL(downloadURL);
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setConnectTimeout(5 * 1000);
|
||||
// 防止屏蔽程序抓取而返回 403 错误
|
||||
conn.setRequestProperty("User-Agent", "Mozilla/31.0 (compatible; MSIE 10.0; Windows NT; DigExt)");
|
||||
|
||||
// 得到输入流
|
||||
InputStream inputStream = conn.getInputStream();
|
||||
// 获取数组
|
||||
inputStream = conn.getInputStream();
|
||||
byte[] data = read(inputStream);
|
||||
|
||||
// 创建文件夹
|
||||
if (!saveDir.exists()) {
|
||||
saveDir.mkdirs();
|
||||
}
|
||||
|
||||
// 保存文件
|
||||
File file = new File(saveDir, filename);
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
|
||||
// 写入文件
|
||||
fos = new FileOutputStream(createNewFile(file));
|
||||
fos.write(data);
|
||||
|
||||
// 结束
|
||||
fos.close();
|
||||
inputStream.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception ignored) {
|
||||
} finally {
|
||||
IOUtils.close(conn);
|
||||
IOUtils.closeQuietly(fos);
|
||||
IOUtils.closeQuietly(inputStream);
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void download(String downloadURL, String filename, File saveDir) {
|
||||
download(downloadURL, new File(saveDir, filename));
|
||||
}
|
||||
|
||||
public static byte[] read(InputStream in) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int len = 0;
|
||||
int len;
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
try {
|
||||
while ((len = in.read(buffer)) != -1) {
|
||||
|
||||
Reference in New Issue
Block a user