合并,删除 .idea 文件夹,数据库
This commit is contained in:
@@ -1,123 +1,116 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
|
||||
public class EncodeUtils {
|
||||
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param fileName 要转换的文件
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(String fileName, String fromCharsetName, String toCharsetName) throws Exception {
|
||||
convert(new File(fileName), fromCharsetName, toCharsetName, null);
|
||||
}
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param fileName 要转换的文件
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(String fileName, String fromCharsetName, String toCharsetName) throws Exception {
|
||||
convert(new File(fileName), fromCharsetName, toCharsetName, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param file 要转换的文件或目录
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(File file, String fromCharsetName, String toCharsetName) throws Exception {
|
||||
convert(file, fromCharsetName, toCharsetName, null);
|
||||
}
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param file 要转换的文件或目录
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(File file, String fromCharsetName, String toCharsetName) throws Exception {
|
||||
convert(file, fromCharsetName, toCharsetName, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 转换的编码
|
||||
* @param filter 文件名过滤器
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(String fileName, String fromCharsetName, String toCharsetName, FilenameFilter filter) throws Exception {
|
||||
convert(new File(fileName), fromCharsetName, toCharsetName, filter);
|
||||
}
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 转换的编码
|
||||
* @param filter 文件名过滤器
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(String fileName, String fromCharsetName, String toCharsetName, FilenameFilter filter) throws Exception {
|
||||
convert(new File(fileName), fromCharsetName, toCharsetName, filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param file 要转换的文件或目录
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @param filter 文件名过滤器
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(File file, String fromCharsetName, String toCharsetName, FilenameFilter filter) throws Exception {
|
||||
if (file.isDirectory()) {
|
||||
File[] fileList = null;
|
||||
if (filter == null) {
|
||||
fileList = file.listFiles();
|
||||
} else {
|
||||
fileList = file.listFiles(filter);
|
||||
}
|
||||
for (File f : fileList) {
|
||||
convert(f, fromCharsetName, toCharsetName, filter);
|
||||
}
|
||||
} else {
|
||||
if (filter == null
|
||||
|| filter.accept(file.getParentFile(), file.getName())) {
|
||||
String fileContent = getFileContentFromCharset(file,
|
||||
fromCharsetName);
|
||||
saveFile2Charset(file, toCharsetName, fileContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 把指定文件或目录转换成指定的编码
|
||||
*
|
||||
* @param file 要转换的文件或目录
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @param filter 文件名过滤器
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void convert(File file, String fromCharsetName, String toCharsetName, FilenameFilter filter) throws Exception {
|
||||
if (file.isDirectory()) {
|
||||
File[] fileList = null;
|
||||
if (filter == null) {
|
||||
fileList = file.listFiles();
|
||||
} else {
|
||||
fileList = file.listFiles(filter);
|
||||
}
|
||||
for (File f : fileList) {
|
||||
convert(f, fromCharsetName, toCharsetName, filter);
|
||||
}
|
||||
} else {
|
||||
if (filter == null
|
||||
|| filter.accept(file.getParentFile(), file.getName())) {
|
||||
String fileContent = getFileContentFromCharset(file,
|
||||
fromCharsetName);
|
||||
saveFile2Charset(file, toCharsetName, fileContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 以指定编码方式读取文件,返回文件内容
|
||||
*
|
||||
* @param file 要转换的文件
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String getFileContentFromCharset(File file, String fromCharsetName) throws Exception {
|
||||
if (!Charset.isSupported(fromCharsetName)) {
|
||||
throw new UnsupportedCharsetException(fromCharsetName);
|
||||
}
|
||||
InputStream inputStream = new FileInputStream(file);
|
||||
InputStreamReader reader = new InputStreamReader(inputStream,
|
||||
fromCharsetName);
|
||||
char[] chs = new char[(int) file.length()];
|
||||
reader.read(chs);
|
||||
String str = new String(chs).trim();
|
||||
reader.close();
|
||||
return str;
|
||||
}
|
||||
/**
|
||||
* 以指定编码方式读取文件,返回文件内容
|
||||
*
|
||||
* @param file 要转换的文件
|
||||
* @param fromCharsetName 源文件的编码
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String getFileContentFromCharset(File file, String fromCharsetName) throws Exception {
|
||||
if (!Charset.isSupported(fromCharsetName)) {
|
||||
throw new UnsupportedCharsetException(fromCharsetName);
|
||||
}
|
||||
InputStream inputStream = new FileInputStream(file);
|
||||
InputStreamReader reader = new InputStreamReader(inputStream,
|
||||
fromCharsetName);
|
||||
char[] chs = new char[(int) file.length()];
|
||||
reader.read(chs);
|
||||
String str = new String(chs).trim();
|
||||
reader.close();
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* 以指定编码方式写文本文件,存在会覆盖
|
||||
*
|
||||
* @param file 要写入的文件
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @param content 文件内容
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void saveFile2Charset(File file, String toCharsetName, String content) throws Exception {
|
||||
if (!Charset.isSupported(toCharsetName)) {
|
||||
throw new UnsupportedCharsetException(toCharsetName);
|
||||
}
|
||||
OutputStream outputStream = new FileOutputStream(file);
|
||||
OutputStreamWriter outWrite = new OutputStreamWriter(outputStream, toCharsetName);
|
||||
outWrite.write(content);
|
||||
outWrite.close();
|
||||
|
||||
System.out.println("[Encodeing...] 更改文件: " + file.getPath());
|
||||
}
|
||||
/**
|
||||
* 以指定编码方式写文本文件,存在会覆盖
|
||||
*
|
||||
* @param file 要写入的文件
|
||||
* @param toCharsetName 要转换的编码
|
||||
* @param content 文件内容
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void saveFile2Charset(File file, String toCharsetName, String content) throws Exception {
|
||||
if (!Charset.isSupported(toCharsetName)) {
|
||||
throw new UnsupportedCharsetException(toCharsetName);
|
||||
}
|
||||
OutputStream outputStream = new FileOutputStream(file);
|
||||
OutputStreamWriter outWrite = new OutputStreamWriter(outputStream, toCharsetName);
|
||||
outWrite.write(content);
|
||||
outWrite.close();
|
||||
|
||||
System.out.println("[Encodeing...] 更改文件: " + file.getPath());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,304 +10,301 @@ import java.net.URLConnection;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
public static String ip() {
|
||||
try {
|
||||
InputStream ins = null;
|
||||
URL url = new URL("http://1212.ip138.com/ic.asp");
|
||||
URLConnection con = url.openConnection();
|
||||
ins = con.getInputStream();
|
||||
|
||||
public static String ip() {
|
||||
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);
|
||||
StringBuilder webContent = new StringBuilder();
|
||||
String str = null;
|
||||
while ((str = bReader.readLine()) != null) {
|
||||
webContent.append(str);
|
||||
}
|
||||
int start = webContent.indexOf("[") + 1;
|
||||
int end = webContent.indexOf("]");
|
||||
String str = null;
|
||||
while ((str = bReader.readLine()) != null) {
|
||||
webContent.append(str);
|
||||
}
|
||||
int start = webContent.indexOf("[") + 1;
|
||||
int end = webContent.indexOf("]");
|
||||
ins.close();
|
||||
return webContent.substring(start, end);
|
||||
}
|
||||
catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
return webContent.substring(start, end);
|
||||
} catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
return "[IP ERROR]";
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并获取文件
|
||||
*
|
||||
* @param filePath
|
||||
* @return
|
||||
*/
|
||||
public static File file(String filePath) {
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并获取文件
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件夹
|
||||
*
|
||||
* @param file
|
||||
*/
|
||||
public void deleteAllFile(File file) {
|
||||
if (!file.exists()) {
|
||||
return;
|
||||
}
|
||||
if (file.isFile()) {
|
||||
file.delete();
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并获取文件
|
||||
*
|
||||
* @param filePath
|
||||
* @return
|
||||
*/
|
||||
public static File file(String filePath) {
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建并获取文件
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件夹
|
||||
*
|
||||
* @param file
|
||||
*/
|
||||
public void deleteAllFile(File file) {
|
||||
if (!file.exists()) {
|
||||
return;
|
||||
}
|
||||
if (file.isFile()) {
|
||||
file.delete();
|
||||
return;
|
||||
}
|
||||
File[] files = file.listFiles();
|
||||
for (File file1 : files) {
|
||||
deleteAllFile(file1);
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制文件夹
|
||||
*
|
||||
file.delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制文件夹
|
||||
*
|
||||
* @param file1 文件1
|
||||
* @param file2 文件2
|
||||
* @throws Exception
|
||||
*/
|
||||
* @throws Exception
|
||||
*/
|
||||
public void copyAllFile(String file1, String file2) throws Exception {
|
||||
File _file1 = new File(file1);
|
||||
File _file2 = new File(file2);
|
||||
if (!_file2.exists()) {
|
||||
if (!_file1.isDirectory()) {
|
||||
_file2.createNewFile();
|
||||
} else {
|
||||
_file2.mkdirs();
|
||||
}
|
||||
if (!_file1.isDirectory()) {
|
||||
_file2.createNewFile();
|
||||
} else {
|
||||
_file2.mkdirs();
|
||||
}
|
||||
}
|
||||
if (_file1.isDirectory()) {
|
||||
for (File file : _file1.listFiles()) {
|
||||
if (file.isDirectory()) {
|
||||
copyAllFile(file.getAbsolutePath(), file2 + "/" + file.getName());
|
||||
} else {
|
||||
fileChannelCopy(file, new File(file2 + "/" + file.getName()));
|
||||
}
|
||||
}
|
||||
for (File file : _file1.listFiles()) {
|
||||
if (file.isDirectory()) {
|
||||
copyAllFile(file.getAbsolutePath(), file2 + "/" + file.getName());
|
||||
} else {
|
||||
fileChannelCopy(file, new File(file2 + "/" + file.getName()));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fileChannelCopy(_file1, _file2);
|
||||
fileChannelCopy(_file1, _file2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 复制文件(通道)
|
||||
*
|
||||
*
|
||||
* @param file1 文件1
|
||||
* @param file2 文件2
|
||||
*/
|
||||
public void fileChannelCopy(File file1, File file2) {
|
||||
FileInputStream fileIn = null;
|
||||
FileOutputStream fileOut = null;
|
||||
FileChannel channelIn = null;
|
||||
FileChannel channelOut = null;
|
||||
try {
|
||||
fileIn = new FileInputStream(file1);
|
||||
fileOut = new FileOutputStream(file2);
|
||||
public void fileChannelCopy(File file1, File file2) {
|
||||
FileInputStream fileIn = null;
|
||||
FileOutputStream fileOut = null;
|
||||
FileChannel channelIn = null;
|
||||
FileChannel channelOut = null;
|
||||
try {
|
||||
fileIn = new FileInputStream(file1);
|
||||
fileOut = new FileOutputStream(file2);
|
||||
channelIn = fileIn.getChannel();
|
||||
channelOut = fileOut.getChannel();
|
||||
channelIn.transferTo(0, channelIn.size(), channelOut);
|
||||
} catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
//
|
||||
} finally {
|
||||
try {
|
||||
fileIn.close();
|
||||
channelIn.close();
|
||||
fileOut.close();
|
||||
channelOut.close();
|
||||
} catch (Exception e) {
|
||||
} finally {
|
||||
try {
|
||||
fileIn.close();
|
||||
channelIn.close();
|
||||
fileOut.close();
|
||||
channelOut.close();
|
||||
} catch (Exception e) {
|
||||
//
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过输入流读取文本
|
||||
*
|
||||
* @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;
|
||||
|
||||
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());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过文件读取文本
|
||||
*
|
||||
* @param file
|
||||
* @param size
|
||||
* @param encode
|
||||
* @return
|
||||
*/
|
||||
public static String getStringFromFile(File file, int size, String encode) {
|
||||
try {
|
||||
FileInputStream fin = new FileInputStream(file);
|
||||
BufferedInputStream bin = new BufferedInputStream(fin);
|
||||
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();
|
||||
fin.close();
|
||||
return new String(bos.toByteArray(), encode);
|
||||
} catch (IOException e) {
|
||||
MsgUtils.warn("文件读取出错: &4" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 URL 读取文本
|
||||
*
|
||||
* @param url
|
||||
* @param size
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过输入流读取文本
|
||||
*
|
||||
* @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;
|
||||
|
||||
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());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过文件读取文本
|
||||
*
|
||||
* @param file
|
||||
* @param size
|
||||
* @param encode
|
||||
* @return
|
||||
*/
|
||||
public static String getStringFromFile(File file, int size, String encode) {
|
||||
try {
|
||||
FileInputStream fin = new FileInputStream(file);
|
||||
BufferedInputStream bin = new BufferedInputStream(fin);
|
||||
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();
|
||||
fin.close();
|
||||
return new String(bos.toByteArray(), encode);
|
||||
} catch (IOException e) {
|
||||
MsgUtils.warn("文件读取出错: &4" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 URL 读取文本
|
||||
*
|
||||
* @param url
|
||||
* @param size
|
||||
* @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;
|
||||
}
|
||||
|
||||
public static String getStringFromURL(String url, String def) {
|
||||
String s = getStringFromURL(url, 1024);
|
||||
return s == null ? def : s;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载文件
|
||||
*
|
||||
*
|
||||
* @param urlStr
|
||||
* @param filename
|
||||
* @param saveDir
|
||||
*/
|
||||
public static void download(String urlStr, String filename, File saveDir) {
|
||||
try {
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection 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();
|
||||
// 获取数组
|
||||
byte[] data = read(inputStream);
|
||||
|
||||
// 创建文件夹
|
||||
if (!saveDir.exists()) {
|
||||
saveDir.mkdirs();
|
||||
}
|
||||
|
||||
// 保存文件
|
||||
File file = new File(saveDir, filename);
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
|
||||
// 写入文件
|
||||
fos.write(data);
|
||||
|
||||
// 结束
|
||||
fos.close();
|
||||
inputStream.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] read(InputStream in) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int len = 0;
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
try {
|
||||
while((len = in.read(buffer)) != -1) {
|
||||
bos.write(buffer, 0, len);
|
||||
}
|
||||
}
|
||||
catch (Exception ignored) {
|
||||
}
|
||||
return bos.toByteArray();
|
||||
}
|
||||
|
||||
public static void close(Closeable closeable) {
|
||||
try {
|
||||
if (closeable != null) {
|
||||
closeable.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
try {
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection 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();
|
||||
// 获取数组
|
||||
byte[] data = read(inputStream);
|
||||
|
||||
// 创建文件夹
|
||||
if (!saveDir.exists()) {
|
||||
saveDir.mkdirs();
|
||||
}
|
||||
|
||||
// 保存文件
|
||||
File file = new File(saveDir, filename);
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
|
||||
// 写入文件
|
||||
fos.write(data);
|
||||
|
||||
// 结束
|
||||
fos.close();
|
||||
inputStream.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] read(InputStream in) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int len = 0;
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
try {
|
||||
while ((len = in.read(buffer)) != -1) {
|
||||
bos.write(buffer, 0, len);
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return bos.toByteArray();
|
||||
}
|
||||
|
||||
public static void close(Closeable closeable) {
|
||||
try {
|
||||
if (closeable != null) {
|
||||
closeable.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,58 +1,47 @@
|
||||
package me.skymc.taboolib.fileutils;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.other.DateUtils;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.other.DateUtils;
|
||||
|
||||
@Deprecated
|
||||
public class LogUtils {
|
||||
|
||||
public static void Log(String s, String s2)
|
||||
{
|
||||
try
|
||||
{
|
||||
File file = new File(Main.getInst().getDataFolder(), s2+".txt");
|
||||
if(!file.exists())
|
||||
{
|
||||
|
||||
public static void Log(String s, String s2) {
|
||||
try {
|
||||
File file = new File(Main.getInst().getDataFolder(), s2 + ".txt");
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
}
|
||||
|
||||
|
||||
FileWriter fileWritter = new FileWriter(file, true);
|
||||
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
|
||||
bufferWritter.write(s);
|
||||
bufferWritter.newLine();
|
||||
bufferWritter.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.getInst().getLogger().warning(s2+":"+s);
|
||||
} catch (Exception e) {
|
||||
Main.getInst().getLogger().warning(s2 + ":" + s);
|
||||
}
|
||||
}
|
||||
|
||||
public static void newLog(Plugin main, String s, String s2)
|
||||
{
|
||||
try
|
||||
{
|
||||
File file = new File(main.getDataFolder(), s2+".txt");
|
||||
if(!file.exists())
|
||||
{
|
||||
|
||||
public static void newLog(Plugin main, String s, String s2) {
|
||||
try {
|
||||
File file = new File(main.getDataFolder(), s2 + ".txt");
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
}
|
||||
|
||||
|
||||
FileWriter fileWritter = new FileWriter(file, true);
|
||||
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
|
||||
bufferWritter.write("["+DateUtils.CH_ALL.format(System.currentTimeMillis())+"]"+s);
|
||||
bufferWritter.write("[" + DateUtils.CH_ALL.format(System.currentTimeMillis()) + "]" + s);
|
||||
bufferWritter.newLine();
|
||||
bufferWritter.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.getInst().getLogger().warning(s2+":"+s);
|
||||
} catch (Exception e) {
|
||||
Main.getInst().getLogger().warning(s2 + ":" + s);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user