Commit 33051cde authored by 张永's avatar 张永

加一个

parent a80a68f1
...@@ -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;
...@@ -89,7 +93,7 @@ public class FileUtil { ...@@ -89,7 +93,7 @@ public class FileUtil {
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;
...@@ -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) {
...@@ -196,13 +200,14 @@ public class FileUtil { ...@@ -196,13 +200,14 @@ public class FileUtil {
/** /**
* 将字节数组转化为文件 * 将字节数组转化为文件
*
* @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;
...@@ -230,13 +235,14 @@ public class FileUtil { ...@@ -230,13 +235,14 @@ public class FileUtil {
/** /**
* 将字节数组转化为文件 * 将字节数组转化为文件
*
* @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;
...@@ -244,6 +250,7 @@ public class FileUtil { ...@@ -244,6 +250,7 @@ public class FileUtil {
/** /**
* 将文件转化为字节数组 * 将文件转化为字节数组
*
* @param file * @param file
* @return * @return
*/ */
...@@ -268,12 +275,13 @@ public class FileUtil { ...@@ -268,12 +275,13 @@ public class FileUtil {
/** /**
* 将文件转化为字节数组 * 将文件转化为字节数组
*
* @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;
...@@ -322,17 +330,18 @@ public class FileUtil { ...@@ -322,17 +330,18 @@ public class FileUtil {
/** /**
* 移动文件 * 移动文件
*
* @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);
} }
...@@ -343,10 +352,11 @@ public class FileUtil { ...@@ -343,10 +352,11 @@ public class FileUtil {
/** /**
* 删除文件 * 删除文件
*
* @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();
...@@ -398,13 +408,13 @@ public class FileUtil { ...@@ -398,13 +408,13 @@ public class FileUtil {
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) {
...@@ -455,10 +465,10 @@ public class FileUtil { ...@@ -455,10 +465,10 @@ public class FileUtil {
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) {
...@@ -472,7 +482,7 @@ public class FileUtil { ...@@ -472,7 +482,7 @@ public class FileUtil {
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;
...@@ -552,7 +562,6 @@ public class FileUtil { ...@@ -552,7 +562,6 @@ public class FileUtil {
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;
...@@ -576,11 +585,59 @@ public class FileUtil { ...@@ -576,11 +585,59 @@ 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();
......
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