|
@@ -1,5 +1,6 @@
|
|
|
package com.fdkk.sxz.util;
|
|
|
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.github.junrar.Archive;
|
|
|
import com.github.junrar.rarfile.FileHeader;
|
|
@@ -35,7 +36,7 @@ public class FileUtils {
|
|
|
} else {
|
|
|
return;
|
|
|
}
|
|
|
- writeBinary(bt, path);
|
|
|
+ FileUtils.writeBinary(bt, path);
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -58,7 +59,7 @@ public class FileUtils {
|
|
|
bos.close();
|
|
|
}
|
|
|
|
|
|
- public static boolean createDir(String destDirName) {
|
|
|
+ public static boolean createDir(String destDirName) {
|
|
|
File dir = new File(destDirName);
|
|
|
if (dir.exists()) {
|
|
|
System.out.println("创建目录" + destDirName + "失败,目标目录已经存在");
|
|
@@ -80,26 +81,27 @@ public class FileUtils {
|
|
|
|
|
|
/**
|
|
|
* 创建文件
|
|
|
- * @param fileName 文件名称
|
|
|
- * @param fileContent 文件内容
|
|
|
- * @return 是否创建成功,成功则返回true
|
|
|
+ *
|
|
|
+ * @param fileName 文件名称
|
|
|
+ * @param fileContent 文件内容
|
|
|
+ * @return 是否创建成功,成功则返回true
|
|
|
*/
|
|
|
- public static boolean createFile(String path, String fileName,String fileContent){
|
|
|
+ public static boolean createFile(String path, String fileName, String fileContent) {
|
|
|
Boolean bool = false;
|
|
|
- fileNameTemp = path+fileName+".json";//文件路径+名称+文件类型
|
|
|
- File file = new File(fileNameTemp);
|
|
|
+ FileUtils.fileNameTemp = path + fileName + ".json";//文件路径+名称+文件类型
|
|
|
+ File file = new File(FileUtils.fileNameTemp);
|
|
|
try {
|
|
|
- File folder = new File(path);
|
|
|
- if (!folder.exists()){
|
|
|
- folder.mkdirs();
|
|
|
- }
|
|
|
+ File folder = new File(path);
|
|
|
+ if (!folder.exists()) {
|
|
|
+ folder.mkdirs();
|
|
|
+ }
|
|
|
//如果文件不存在,则创建新的文件
|
|
|
- if(!file.exists()){
|
|
|
+ if (!file.exists()) {
|
|
|
file.createNewFile();
|
|
|
bool = true;
|
|
|
- System.out.println("success create file,the file is "+ fileNameTemp);
|
|
|
+ System.out.println("success create file,the file is " + FileUtils.fileNameTemp);
|
|
|
//创建文件成功后,写入内容到文件里
|
|
|
- writeFileContent(fileNameTemp, fileContent);
|
|
|
+ FileUtils.writeFileContent(FileUtils.fileNameTemp, fileContent);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
@@ -110,20 +112,21 @@ public class FileUtils {
|
|
|
|
|
|
/**
|
|
|
* 向文件中写入内容
|
|
|
+ *
|
|
|
* @param filePath 文件路径与名称
|
|
|
- * @param newstr 写入的内容
|
|
|
+ * @param newstr 写入的内容
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- public static boolean writeFileContent(String filePath, String newstr) throws IOException{
|
|
|
+ public static boolean writeFileContent(String filePath, String newstr) throws IOException {
|
|
|
Boolean bool = false;
|
|
|
- String filein = newstr+"\r\n";//新写入的行,换行
|
|
|
- String temp = "";
|
|
|
+ String filein = newstr + "\r\n";//新写入的行,换行
|
|
|
+ String temp = "";
|
|
|
|
|
|
FileInputStream fis = null;
|
|
|
InputStreamReader isr = null;
|
|
|
BufferedReader br = null;
|
|
|
- FileOutputStream fos = null;
|
|
|
+ FileOutputStream fos = null;
|
|
|
PrintWriter pw = null;
|
|
|
try {
|
|
|
File file = new File(filePath);//文件路径(包括文件名称)
|
|
@@ -134,7 +137,7 @@ public class FileUtils {
|
|
|
StringBuffer buffer = new StringBuffer();
|
|
|
|
|
|
//文件原有内容
|
|
|
- for(int i=0;(temp =br.readLine())!=null;i++){
|
|
|
+ for (int i = 0; (temp = br.readLine()) != null; i++) {
|
|
|
buffer.append(temp);
|
|
|
// 行与行之间的分隔符 相当于“\n”
|
|
|
buffer = buffer.append(System.getProperty("line.separator"));
|
|
@@ -149,7 +152,7 @@ public class FileUtils {
|
|
|
} catch (Exception e) {
|
|
|
// TODO: handle exception
|
|
|
e.printStackTrace();
|
|
|
- }finally {
|
|
|
+ } finally {
|
|
|
//不要忘记关闭
|
|
|
if (pw != null) {
|
|
|
pw.close();
|
|
@@ -173,8 +176,7 @@ public class FileUtils {
|
|
|
/**
|
|
|
* 删除单个文件
|
|
|
*
|
|
|
- * @param fileName
|
|
|
- * 要删除的文件的文件名
|
|
|
+ * @param fileName 要删除的文件的文件名
|
|
|
* @return 单个文件删除成功返回true,否则返回false
|
|
|
*/
|
|
|
public static boolean deleteFile(String fileName) {
|
|
@@ -191,9 +193,10 @@ public class FileUtils {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 根据路径删除指定的目录,无论存在与否
|
|
|
- *@param sPath 要删除的目录path
|
|
|
- *@return 删除成功返回 true,否则返回 false。
|
|
|
+ * 根据路径删除指定的目录,无论存在与否
|
|
|
+ *
|
|
|
+ * @param sPath 要删除的目录path
|
|
|
+ * @return 删除成功返回 true,否则返回 false。
|
|
|
*/
|
|
|
public static boolean deleteFolder(String sPath) {
|
|
|
boolean flag = false;
|
|
@@ -204,17 +207,18 @@ public class FileUtils {
|
|
|
} else {
|
|
|
// 判断是否为文件
|
|
|
if (file.isFile()) { // 为文件时调用删除文件方法
|
|
|
- return deleteFile(sPath);
|
|
|
+ return FileUtils.deleteFile(sPath);
|
|
|
} else { // 为目录时调用删除目录方法
|
|
|
- return deleteDirectory(sPath);
|
|
|
+ return FileUtils.deleteDirectory(sPath);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除目录以及目录下的文件
|
|
|
- * @param sPath 被删除目录的路径
|
|
|
- * @return 目录删除成功返回true,否则返回false
|
|
|
+ *
|
|
|
+ * @param sPath 被删除目录的路径
|
|
|
+ * @return 目录删除成功返回true,否则返回false
|
|
|
*/
|
|
|
public static boolean deleteDirectory(String sPath) {
|
|
|
//如果sPath不以文件分隔符结尾,自动添加文件分隔符
|
|
@@ -232,15 +236,21 @@ public class FileUtils {
|
|
|
for (int i = 0; i < files.length; i++) {
|
|
|
//删除子文件
|
|
|
if (files[i].isFile()) {
|
|
|
- flag = deleteFile(files[i].getAbsolutePath());
|
|
|
- if (!flag) break;
|
|
|
+ flag = FileUtils.deleteFile(files[i].getAbsolutePath());
|
|
|
+ if (!flag) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
} //删除子目录
|
|
|
else {
|
|
|
- flag = deleteDirectory(files[i].getAbsolutePath());
|
|
|
- if (!flag) break;
|
|
|
+ flag = FileUtils.deleteDirectory(files[i].getAbsolutePath());
|
|
|
+ if (!flag) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- if (!flag) return false;
|
|
|
+ if (!flag) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
//删除当前目录
|
|
|
if (dirFile.delete()) {
|
|
|
return true;
|
|
@@ -323,10 +333,12 @@ public class FileUtils {
|
|
|
return false;
|
|
|
} finally {
|
|
|
try {
|
|
|
- if (out != null)
|
|
|
+ if (out != null) {
|
|
|
out.close();
|
|
|
- if (in != null)
|
|
|
+ }
|
|
|
+ if (in != null) {
|
|
|
in.close();
|
|
|
+ }
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -342,7 +354,7 @@ public class FileUtils {
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- public static boolean downLoadFromUrl(String urlStr, String fileName, String savePath){
|
|
|
+ public static boolean downLoadFromUrl(String urlStr, String fileName, String savePath) {
|
|
|
FileOutputStream fos = null;
|
|
|
InputStream inputStream = null;
|
|
|
try {
|
|
@@ -356,7 +368,7 @@ public class FileUtils {
|
|
|
// 得到输入流
|
|
|
inputStream = conn.getInputStream();
|
|
|
// 获取自己数组
|
|
|
- byte[] getData = readInputStream(inputStream);
|
|
|
+ byte[] getData = FileUtils.readInputStream(inputStream);
|
|
|
|
|
|
// 文件保存位置
|
|
|
File saveDir = new File(savePath);
|
|
@@ -376,24 +388,24 @@ public class FileUtils {
|
|
|
if (inputStream != null) {
|
|
|
inputStream.close();
|
|
|
}
|
|
|
- log.info("info:" + url + " download success");
|
|
|
- } catch(FileNotFoundException e){
|
|
|
+ FileUtils.log.info("info:" + url + " download success");
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
return false;
|
|
|
} catch (IOException e) {
|
|
|
return false;
|
|
|
- }finally {
|
|
|
+ } finally {
|
|
|
if (fos != null) {
|
|
|
try {
|
|
|
fos.close();
|
|
|
} catch (IOException e) {
|
|
|
- log.error("下载文件异常", e);
|
|
|
+ FileUtils.log.error("下载文件异常", e);
|
|
|
}
|
|
|
}
|
|
|
if (inputStream != null) {
|
|
|
try {
|
|
|
inputStream.close();
|
|
|
} catch (IOException e) {
|
|
|
- log.error("下载文件异常", e);
|
|
|
+ FileUtils.log.error("下载文件异常", e);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -418,18 +430,8 @@ public class FileUtils {
|
|
|
return bos.toByteArray();
|
|
|
}
|
|
|
|
|
|
- public static void writeFile(String filePath,String str) throws IOException {
|
|
|
- File fout = new File(filePath);
|
|
|
- if(!fout.getParentFile().exists()){
|
|
|
- fout.getParentFile().mkdirs();
|
|
|
- }
|
|
|
- if(!fout.exists()){
|
|
|
- fout.createNewFile();
|
|
|
- }
|
|
|
- FileOutputStream fos = new FileOutputStream(fout);
|
|
|
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
|
|
|
- bw.write(str);
|
|
|
- bw.close();
|
|
|
+ public static void writeFile(String filePath, String str) throws IOException {
|
|
|
+ FileUtil.writeUtf8String(str, filePath);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -457,22 +459,20 @@ public class FileUtils {
|
|
|
|
|
|
/**
|
|
|
* 向json文件写入参数,重复的覆盖,多的新增
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
- public static void writeJsonFile(String path, Map<String, Object> map) throws Exception{
|
|
|
- String str = readFile(path);
|
|
|
+ public static void writeJsonFile(String path, Map<String, Object> map) throws Exception {
|
|
|
+ String str = FileUtils.readFile(path);
|
|
|
JSONObject json = new JSONObject();
|
|
|
- if(str!=null){
|
|
|
+ if (str != null) {
|
|
|
json = JSONObject.parseObject(str);
|
|
|
- }
|
|
|
- else{
|
|
|
+ } else {
|
|
|
File file = new File(path);
|
|
|
- if(!file.getParentFile().exists())
|
|
|
- {
|
|
|
+ if (!file.getParentFile().exists()) {
|
|
|
file.getParentFile().mkdirs();
|
|
|
}
|
|
|
- if(!file.exists())
|
|
|
- {
|
|
|
+ if (!file.exists()) {
|
|
|
file.createNewFile();
|
|
|
}
|
|
|
}
|
|
@@ -482,14 +482,14 @@ public class FileUtils {
|
|
|
json.put(String.valueOf(entry.getKey()), entry.getValue());
|
|
|
}
|
|
|
|
|
|
- writeFile(path, json.toString());
|
|
|
+ FileUtils.writeFile(path, json.toString());
|
|
|
}
|
|
|
|
|
|
|
|
|
//删除文件夹
|
|
|
public static void delFolder(String folderPath) {
|
|
|
try {
|
|
|
- delAllFile(folderPath); //删除完里面所有内容
|
|
|
+ FileUtils.delAllFile(folderPath); //删除完里面所有内容
|
|
|
String filePath = folderPath;
|
|
|
filePath = filePath.toString();
|
|
|
File myFilePath = new File(filePath);
|
|
@@ -511,8 +511,7 @@ public class FileUtils {
|
|
|
}
|
|
|
String[] tempList = file.list();
|
|
|
File temp = null;
|
|
|
- if(tempList!=null)
|
|
|
- {
|
|
|
+ if (tempList != null) {
|
|
|
for (int i = 0; i < tempList.length; i++) {
|
|
|
if (path.endsWith(File.separator)) {
|
|
|
temp = new File(path + tempList[i]);
|
|
@@ -523,8 +522,8 @@ public class FileUtils {
|
|
|
temp.delete();
|
|
|
}
|
|
|
if (temp.isDirectory()) {
|
|
|
- delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
|
|
|
- delFolder(path + "/" + tempList[i]);//再删除空文件夹
|
|
|
+ FileUtils.delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
|
|
|
+ FileUtils.delFolder(path + "/" + tempList[i]);//再删除空文件夹
|
|
|
flag = true;
|
|
|
}
|
|
|
}
|
|
@@ -535,20 +534,15 @@ public class FileUtils {
|
|
|
return flag;
|
|
|
}
|
|
|
|
|
|
- public static List<String> readfileNamesForDirectory(String path, String except)
|
|
|
- {
|
|
|
- try{
|
|
|
+ public static List<String> readfileNamesForDirectory(String path, String except) {
|
|
|
+ try {
|
|
|
File file = new File(path);
|
|
|
- if(file.isDirectory())
|
|
|
- {
|
|
|
+ if (file.isDirectory()) {
|
|
|
String[] fileNames = file.list();
|
|
|
List<String> list = new ArrayList<String>();
|
|
|
- if(fileNames!=null)
|
|
|
- {
|
|
|
- for(int i=0;i<fileNames.length;++i)
|
|
|
- {
|
|
|
- if(fileNames[i].toLowerCase().endsWith(except) )
|
|
|
- {
|
|
|
+ if (fileNames != null) {
|
|
|
+ for (int i = 0; i < fileNames.length; ++i) {
|
|
|
+ if (fileNames[i].toLowerCase().endsWith(except)) {
|
|
|
list.add(fileNames[i]);
|
|
|
}
|
|
|
}
|
|
@@ -556,7 +550,7 @@ public class FileUtils {
|
|
|
|
|
|
return list;
|
|
|
}
|
|
|
- }catch(Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return null;
|
|
@@ -564,35 +558,35 @@ public class FileUtils {
|
|
|
|
|
|
//递归获取文件中所有文件的路径
|
|
|
public static List<String> readfilePath(String path, List<String> urlList) {
|
|
|
- try{
|
|
|
+ try {
|
|
|
File file = new File(path);
|
|
|
- if(file != null && file.isDirectory()) {
|
|
|
+ if (file != null && file.isDirectory()) {
|
|
|
File[] files = file.listFiles();
|
|
|
|
|
|
- if(files != null) {
|
|
|
- for(int i=0;i<files.length;++i) {
|
|
|
- if(files[i].isDirectory()){
|
|
|
- readfilePath(files[i].getAbsolutePath(), urlList);
|
|
|
- }else {
|
|
|
+ if (files != null) {
|
|
|
+ for (int i = 0; i < files.length; ++i) {
|
|
|
+ if (files[i].isDirectory()) {
|
|
|
+ FileUtils.readfilePath(files[i].getAbsolutePath(), urlList);
|
|
|
+ } else {
|
|
|
urlList.add(files[i].getAbsolutePath());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return urlList;
|
|
|
}
|
|
|
- }catch(Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return urlList;
|
|
|
}
|
|
|
|
|
|
- public static void saveImageToDisk(String accessToken, String mediaId, String picName, String picPath,InputStream inputStream)
|
|
|
+ public static void saveImageToDisk(String accessToken, String mediaId, String picName, String picPath, InputStream inputStream)
|
|
|
throws Exception {
|
|
|
byte[] data = new byte[10240];
|
|
|
int len = 0;
|
|
|
FileOutputStream fileOutputStream = null;
|
|
|
try {
|
|
|
- fileOutputStream = new FileOutputStream(picPath+picName+".amr");
|
|
|
+ fileOutputStream = new FileOutputStream(picPath + picName + ".amr");
|
|
|
while ((len = inputStream.read(data)) != -1) {
|
|
|
fileOutputStream.write(data, 0, len);
|
|
|
}
|
|
@@ -617,14 +611,12 @@ public class FileUtils {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
- *
|
|
|
* @param content base64内容
|
|
|
- * @param path 输出文件路径,需要后缀名
|
|
|
+ * @param path 输出文件路径,需要后缀名
|
|
|
* @return
|
|
|
*/
|
|
|
- public static boolean base64ToFileWriter(String content, String path) {
|
|
|
+ public static boolean base64ToFileWriter(String content, String path) {
|
|
|
if (content == null) {
|
|
|
return false;
|
|
|
}
|
|
@@ -651,11 +643,11 @@ public class FileUtils {
|
|
|
/**
|
|
|
* 获取类路径(classes路径)
|
|
|
*/
|
|
|
- public static String getResource(){
|
|
|
+ public static String getResource() {
|
|
|
String path = "";
|
|
|
try {
|
|
|
path = ResourceUtils.getURL("classpath:").getPath();
|
|
|
- path = URLDecoder.decode(path,"utf-8");
|
|
|
+ path = URLDecoder.decode(path, "utf-8");
|
|
|
} catch (Exception e) {
|
|
|
}
|
|
|
return path;
|
|
@@ -664,7 +656,7 @@ public class FileUtils {
|
|
|
/**
|
|
|
* 判断文件大小处于限制内
|
|
|
*
|
|
|
- * @param fileLen 文件长度
|
|
|
+ * @param fileLen 文件长度
|
|
|
* @param fileSize 限制大小
|
|
|
* @param fileUnit 限制的单位(B,K,M,G)
|
|
|
* @return
|
|
@@ -677,9 +669,9 @@ public class FileUtils {
|
|
|
} else if ("K".equals(fileUnit.toUpperCase())) {
|
|
|
fileSizeCom = (double) fileLen / 1024;
|
|
|
} else if ("M".equals(fileUnit.toUpperCase())) {
|
|
|
- fileSizeCom = (double) fileLen / (1024*1024);
|
|
|
+ fileSizeCom = (double) fileLen / (1024 * 1024);
|
|
|
} else if ("G".equals(fileUnit.toUpperCase())) {
|
|
|
- fileSizeCom = (double) fileLen / (1024*1024*1024);
|
|
|
+ fileSizeCom = (double) fileLen / (1024 * 1024 * 1024);
|
|
|
}
|
|
|
if (fileSizeCom > fileSize) {
|
|
|
return false;
|
|
@@ -708,7 +700,7 @@ public class FileUtils {
|
|
|
|
|
|
entry = (ZipEntry) entries.nextElement();
|
|
|
|
|
|
- log.info("解压" + entry.getName());
|
|
|
+ FileUtils.log.info("解压" + entry.getName());
|
|
|
|
|
|
if (entry.isDirectory()) {
|
|
|
|
|
@@ -727,7 +719,7 @@ public class FileUtils {
|
|
|
if (!f.exists()) {
|
|
|
|
|
|
//String dirs = FileUtils.getParentPath(f);
|
|
|
- String dirs = f.getParent();
|
|
|
+ String dirs = f.getParent();
|
|
|
|
|
|
File parentDir = new File(dirs);
|
|
|
|
|
@@ -744,7 +736,6 @@ public class FileUtils {
|
|
|
FileOutputStream fos = new FileOutputStream(f);
|
|
|
|
|
|
|
|
|
-
|
|
|
int count;
|
|
|
|
|
|
byte[] buf = new byte[8192];
|
|
@@ -813,13 +804,13 @@ public class FileUtils {
|
|
|
}
|
|
|
|
|
|
BufferedReader buf = null;
|
|
|
- OutputStreamWriter pw=null;
|
|
|
+ OutputStreamWriter pw = null;
|
|
|
String str = null;
|
|
|
- String allstr="";
|
|
|
+ String allstr = "";
|
|
|
|
|
|
buf = new BufferedReader(new InputStreamReader(new FileInputStream(filepath), "GBK"));
|
|
|
- while((str = buf.readLine()) != null){
|
|
|
- allstr=allstr+str;
|
|
|
+ while ((str = buf.readLine()) != null) {
|
|
|
+ allstr = allstr + str;
|
|
|
}
|
|
|
|
|
|
buf.close();
|
|
@@ -827,7 +818,7 @@ public class FileUtils {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public static void main(String[] args) throws Exception{
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
// File f = new File("F:\\桌面\\vr-t-2KZ4MQv");
|
|
|
// for(File f1 : f.listFiles()){
|
|
|
// f1.renameTo(new File(f1.getAbsolutePath().replace(".png", ".jpg")));
|