Commit 56410421 authored by Quxl's avatar Quxl
parents 91c1da32 a2463789
...@@ -14,9 +14,13 @@ import java.io.IOException; ...@@ -14,9 +14,13 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.io.Writer; import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.HashMap; import java.util.HashMap;
...@@ -77,7 +81,7 @@ public class FileUtil { ...@@ -77,7 +81,7 @@ public class FileUtil {
throw new FileUtilException(e); throw new FileUtilException(e);
} }
} }
public final static String contentType(String fullName) { public final static String contentType(String fullName) {
try { try {
return Files.probeContentType(Paths.get(fullName)); return Files.probeContentType(Paths.get(fullName));
...@@ -85,16 +89,16 @@ public class FileUtil { ...@@ -85,16 +89,16 @@ public class FileUtil {
throw new FileUtilException(e); throw new FileUtilException(e);
} }
} }
public final static String getExtName(File file) { public final static String getExtName(File file) {
String extName = null; String extName = null;
String fileName = file.getName(); String fileName = file.getName();
if(fileName.contains(".")) { if (fileName.contains(".")) {
extName = fileName.substring(fileName.lastIndexOf(".") + 1).toUpperCase(); extName = fileName.substring(fileName.lastIndexOf(".") + 1).toUpperCase();
} }
return extName; return extName;
} }
public final static String imageType(File imageFile) { public final static String imageType(File imageFile) {
if (isImage(imageFile)) { if (isImage(imageFile)) {
ImageInputStream iis = null; ImageInputStream iis = null;
...@@ -126,7 +130,7 @@ public class FileUtil { ...@@ -126,7 +130,7 @@ public class FileUtil {
InputStream is = null; InputStream is = null;
try { try {
String extName = getExtName(file); String extName = getExtName(file);
if(StringUtil.isNotBlank(extName)) { if (StringUtil.isNotBlank(extName)) {
return extName; return extName;
} else { } else {
is = new FileInputStream(file); is = new FileInputStream(file);
...@@ -137,7 +141,7 @@ public class FileUtil { ...@@ -137,7 +141,7 @@ public class FileUtil {
return getExtName(file); return getExtName(file);
} finally { } finally {
try { try {
if(is != null) { if (is != null) {
is.close(); is.close();
} }
} catch (IOException e) { } catch (IOException e) {
...@@ -145,7 +149,7 @@ public class FileUtil { ...@@ -145,7 +149,7 @@ public class FileUtil {
} }
} }
} }
public final static String fileType(byte[] b) { public final static String fileType(byte[] b) {
String filetypeHex = String.valueOf(fileHex(b)); String filetypeHex = String.valueOf(fileHex(b));
Iterator<Entry<String, String>> entryiterator = FILE_TYPE_MAP.entrySet().iterator(); Iterator<Entry<String, String>> entryiterator = FILE_TYPE_MAP.entrySet().iterator();
...@@ -193,16 +197,17 @@ public class FileUtil { ...@@ -193,16 +197,17 @@ public class FileUtil {
} }
return stringBuilder.toString(); return stringBuilder.toString();
} }
/** /**
* 将字节数组转化为文件 * 将字节数组转化为文件
*
* @param bytes * @param bytes
* @param filename * @param filename
* @return * @return
*/ */
public static File bytesToFile(byte[] bytes, File file) { public static File bytesToFile(byte[] bytes, File file) {
File parent = file.getParentFile(); File parent = file.getParentFile();
if(!parent.exists()) { if (!parent.exists()) {
parent.mkdirs(); parent.mkdirs();
} }
BufferedOutputStream stream = null; BufferedOutputStream stream = null;
...@@ -223,27 +228,29 @@ public class FileUtil { ...@@ -223,27 +228,29 @@ public class FileUtil {
} }
return file; return file;
} }
public static File bytesToFile(byte[] bytes, String filename) { public static File bytesToFile(byte[] bytes, String filename) {
return bytesToFile(bytes, new File(filename)); return bytesToFile(bytes, new File(filename));
} }
/** /**
* 将字节数组转化为文件 * 将字节数组转化为文件
*
* @param bytes * @param bytes
* @param filenames * @param filenames
* @return * @return
*/ */
public static File[] bytesToFiles(byte[][] bytes, String[] filenames) { public static File[] bytesToFiles(byte[][] bytes, String[] filenames) {
File[] files = new File[filenames.length]; File[] files = new File[filenames.length];
for(int i = 0; i < filenames.length; i++) { for (int i = 0; i < filenames.length; i++) {
files[i] = bytesToFile(bytes[i], filenames[i]); files[i] = bytesToFile(bytes[i], filenames[i]);
} }
return files; return files;
} }
/** /**
* 将文件转化为字节数组 * 将文件转化为字节数组
*
* @param file * @param file
* @return * @return
*/ */
...@@ -265,28 +272,29 @@ public class FileUtil { ...@@ -265,28 +272,29 @@ public class FileUtil {
} }
return bytes; return bytes;
} }
/** /**
* 将文件转化为字节数组 * 将文件转化为字节数组
*
* @param files * @param files
* @return * @return
*/ */
public static byte[][] filesToBytes(File[] files) { public static byte[][] filesToBytes(File[] files) {
byte[][] datas = new byte[files.length][]; byte[][] datas = new byte[files.length][];
for(int i = 0; i < files.length; i++) { for (int i = 0; i < files.length; i++) {
datas[i] = fileToBytes(files[i]); datas[i] = fileToBytes(files[i]);
} }
return datas; return datas;
} }
/** /**
* 复制文件或文件夹 * 复制文件或文件夹
* *
* @param src 源文件 * @param src 源文件
* @param des 目标文件 * @param des 目标文件
* @throws IOException 异常时抛出 * @throws IOException 异常时抛出
*/ */
public static void fileCopy(File src, File des) { public static void fileCopy(File src, File des) {
if (!src.exists()) { if (!src.exists()) {
return; return;
} }
...@@ -318,35 +326,37 @@ public class FileUtil { ...@@ -318,35 +326,37 @@ public class FileUtil {
fileCopy(sf, new File(des.getAbsolutePath() + File.separator + sf.getName())); fileCopy(sf, new File(des.getAbsolutePath() + File.separator + sf.getName()));
} }
} }
} }
/** /**
* 移动文件 * 移动文件
*
* @param src 原文件 * @param src 原文件
* @param des 目标文件 * @param des 目标文件
* @throws IOException * @throws IOException
*/ */
public static void fileMove(File src, File des) { public static void fileMove(File src, File des) {
if(src != null) { if (src != null) {
File parent = des.getParentFile(); File parent = des.getParentFile();
if(!parent.exists()) { if (!parent.exists()) {
parent.mkdirs(); parent.mkdirs();
} }
if(!src.renameTo(des)) { if (!src.renameTo(des)) {
fileCopy(src, des); fileCopy(src, des);
fileDelete(src); fileDelete(src);
} }
} else { } else {
throw new FileUtilException("要移动的源文件不存在:" + src); throw new FileUtilException("要移动的源文件不存在:" + src);
} }
} }
/** /**
* 删除文件 * 删除文件
*
* @param file 目标文件 * @param file 目标文件
*/ */
public static void fileDelete(File... files) { public static void fileDelete(File... files) {
for(File file : files) { for (File file : files) {
if (file.exists()) { if (file.exists()) {
if (file.isFile()) { if (file.isFile()) {
file.delete(); file.delete();
...@@ -359,7 +369,7 @@ public class FileUtil { ...@@ -359,7 +369,7 @@ public class FileUtil {
} }
} }
} }
public static byte[] streamToBytes(InputStream instream) { public static byte[] streamToBytes(InputStream instream) {
byte[] bytes = null; byte[] bytes = null;
ByteArrayOutputStream baos = null; ByteArrayOutputStream baos = null;
...@@ -382,29 +392,29 @@ public class FileUtil { ...@@ -382,29 +392,29 @@ public class FileUtil {
} }
return bytes; return bytes;
} }
public static File streamToFile(InputStream instream, File file) { public static File streamToFile(InputStream instream, File file) {
FileUtil.createFile(file); FileUtil.createFile(file);
byte[] bytes = FileUtil.streamToBytes(instream); byte[] bytes = FileUtil.streamToBytes(instream);
return FileUtil.bytesToFile(bytes, file); return FileUtil.bytesToFile(bytes, file);
} }
public static File streamToFile(InputStream instream, String fileName) { public static File streamToFile(InputStream instream, String fileName) {
return FileUtil.streamToFile(instream, new File(fileName)); return FileUtil.streamToFile(instream, new File(fileName));
} }
public static void stringToFile(File file, String... strings) { public static void stringToFile(File file, String... strings) {
FileOutputStream fos = null; FileOutputStream fos = null;
OutputStreamWriter osw = null; OutputStreamWriter osw = null;
Writer writer = null; Writer writer = null;
try { try {
if(!file.getParentFile().exists()) { if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
} }
fos = new FileOutputStream(file); fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos, "UTF-8"); osw = new OutputStreamWriter(fos, "UTF-8");
writer = new BufferedWriter(osw); writer = new BufferedWriter(osw);
for(String string : strings) { for (String string : strings) {
writer.append(string).append(System.getProperty("line.separator")); writer.append(string).append(System.getProperty("line.separator"));
} }
} catch (IOException e) { } catch (IOException e) {
...@@ -419,11 +429,11 @@ public class FileUtil { ...@@ -419,11 +429,11 @@ public class FileUtil {
} }
} }
} }
public static void stringToFile(String fileFullName, String... strings) { public static void stringToFile(String fileFullName, String... strings) {
FileUtil.stringToFile(new File(fileFullName), strings); FileUtil.stringToFile(new File(fileFullName), strings);
} }
public static String fileToString(File file, String charset) { public static String fileToString(File file, String charset) {
try { try {
return new String(FileUtil.fileToBytes(file), charset); return new String(FileUtil.fileToBytes(file), charset);
...@@ -431,34 +441,34 @@ public class FileUtil { ...@@ -431,34 +441,34 @@ public class FileUtil {
throw new FileUtilException("Unsupported charset:" + charset, e); throw new FileUtilException("Unsupported charset:" + charset, e);
} }
} }
public static String fileToString(String fileFullName, String charset) { public static String fileToString(String fileFullName, String charset) {
return FileUtil.fileToString(new File(fileFullName), charset); return FileUtil.fileToString(new File(fileFullName), charset);
} }
public static String fileToString(File file) { public static String fileToString(File file) {
return FileUtil.fileToString(file, "UTF-8"); return FileUtil.fileToString(file, "UTF-8");
} }
public static String fileToString(String fileFullName) { public static String fileToString(String fileFullName) {
return FileUtil.fileToString(new File(fileFullName)); return FileUtil.fileToString(new File(fileFullName));
} }
public static File fileFromClasspath(String name) { public static File fileFromClasspath(String name) {
String fullName = FileUtil.class.getResource(name).getPath(); String fullName = FileUtil.class.getResource(name).getPath();
return new File(fullName); return new File(fullName);
} }
public static File createFile(String name) { public static File createFile(String name) {
return FileUtil.createFile(new File(name)); return FileUtil.createFile(new File(name));
} }
public static File createFile(File file) { public static File createFile(File file) {
File folder = file.getParentFile(); File folder = file.getParentFile();
if(!(folder.exists() && folder.isDirectory())) { if (!(folder.exists() && folder.isDirectory())) {
folder.mkdirs(); folder.mkdirs();
} }
if(!file.exists() || file.isDirectory()) { if (!file.exists() || file.isDirectory()) {
try { try {
file.createNewFile(); file.createNewFile();
} catch (IOException e) { } catch (IOException e) {
...@@ -469,15 +479,15 @@ public class FileUtil { ...@@ -469,15 +479,15 @@ public class FileUtil {
} }
return file; return file;
} }
public static File createFolder(String name) { public static File createFolder(String name) {
File folder = new File(name); File folder = new File(name);
if(!folder.exists() || folder.isFile()) { if (!folder.exists() || folder.isFile()) {
folder.mkdirs(); folder.mkdirs();
} }
return folder; return folder;
} }
public static void objectToFile(Object obj, File file) { public static void objectToFile(Object obj, File file) {
FileOutputStream fos = null; FileOutputStream fos = null;
ObjectOutputStream oos = null; ObjectOutputStream oos = null;
...@@ -498,22 +508,22 @@ public class FileUtil { ...@@ -498,22 +508,22 @@ public class FileUtil {
} }
} }
} }
public static InputStream bytesToInputStream(byte[] bytes) { public static InputStream bytesToInputStream(byte[] bytes) {
return new ByteArrayInputStream(bytes); return new ByteArrayInputStream(bytes);
} }
/** /**
* 序列化 * 序列化
* *
* @param object * @param object
* @return * @return
* @throws IOException * @throws IOException
*/ */
public static byte[] objToBytes(Object object) throws IOException { public static byte[] objToBytes(Object object) throws IOException {
ObjectOutputStream oos = null; ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null; ByteArrayOutputStream baos = null;
try { try {
baos = new ByteArrayOutputStream(); baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos); oos = new ObjectOutputStream(baos);
oos.writeObject(object); oos.writeObject(object);
...@@ -525,20 +535,20 @@ public class FileUtil { ...@@ -525,20 +535,20 @@ public class FileUtil {
baos.flush(); baos.flush();
baos.close(); baos.close();
} }
} }
/** /**
* 反序列化 * 反序列化
* *
* @param bytes * @param bytes
* @return * @return
* @throws IOException * @throws IOException
* @throws ClassNotFoundException * @throws ClassNotFoundException
*/ */
public static Object bytesToObj(byte[] bytes) throws IOException, ClassNotFoundException { public static Object bytesToObj(byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream bais = null; ByteArrayInputStream bais = null;
ObjectInputStream ois = null; ObjectInputStream ois = null;
try { try {
bais = new ByteArrayInputStream(bytes); bais = new ByteArrayInputStream(bytes);
ois = new ObjectInputStream(bais); ois = new ObjectInputStream(bais);
return ois.readObject(); return ois.readObject();
...@@ -546,13 +556,12 @@ public class FileUtil { ...@@ -546,13 +556,12 @@ public class FileUtil {
ois.close(); ois.close();
bais.close(); bais.close();
} }
} }
public static boolean isNotExists(String jspname) { public static boolean isNotExists(String jspname) {
return !new File(jspname).exists(); return !new File(jspname).exists();
} }
public static Object fileToObject(File file) { public static Object fileToObject(File file) {
FileInputStream fis = null; FileInputStream fis = null;
ObjectInputStream ois = null; ObjectInputStream ois = null;
...@@ -575,15 +584,63 @@ public class FileUtil { ...@@ -575,15 +584,63 @@ public class FileUtil {
} }
} }
} }
public static void urlToFile(String url, String savePath,String fileName) {
InputStream is = null;
OutputStream os = null;
try {
// 构造URL
URL weburl = new URL(url);
// 打开连接
URLConnection con = weburl.openConnection();
// 设置请求超时为5s
con.setConnectTimeout(5 * 1000);
// 输入流
is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
File sf = new File(savePath);
if (!sf.exists()) {
sf.mkdirs();
}
os = new FileOutputStream(savePath+"/"+fileName);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void appendMethod(String fileName, String content) { public static void appendMethod(String fileName, String content) {
try { try {
//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter writer = new FileWriter(fileName, true); FileWriter writer = new FileWriter(fileName, true);
writer.write(content+"\n"); writer.write(content + "\n");
writer.close(); writer.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment