|
|
|
package com.dxhy.common.util;
|
|
|
|
|
|
|
|
import cn.hutool.core.codec.Base64;
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author jiaohongyang
|
|
|
|
*/
|
|
|
|
public class FileUtil {
|
|
|
|
|
|
|
|
private static boolean flag;
|
|
|
|
private static File file;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 添加内容到指定文件 如果该文件不存在,则创建并添加内容 如果该文件已存在,则添加内容到已有内容最后 flag为true,则向现有文件中添加内容,否则覆盖原有内容
|
|
|
|
*
|
|
|
|
* @throws IOException
|
|
|
|
* 异常信息
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static void writeFile(String path, String fileName, String fileContent) throws IOException {
|
|
|
|
|
|
|
|
if (null == fileContent || "".equals(fileContent)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!directoryIsExists(path)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileWriter writer = null;
|
|
|
|
try {
|
|
|
|
writer = new FileWriter(new File(path + fileName));
|
|
|
|
writer.write(fileContent);
|
|
|
|
writer.flush();
|
|
|
|
} finally {
|
|
|
|
if (null != writer) {
|
|
|
|
try {
|
|
|
|
writer.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean directoryIsExists(String path) {
|
|
|
|
File directory = new File(path);
|
|
|
|
if (directory.exists()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!directory.mkdir()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (directory.canWrite()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return directory.setWritable(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 根据路径删除指定的目录或文件,无论存在与否
|
|
|
|
*
|
|
|
|
* @param sPath
|
|
|
|
* 要删除的目录或文件 删除成功返回 true,否则返回 false。
|
|
|
|
*/
|
|
|
|
public static void deleteFolder(String sPath) {
|
|
|
|
flag = false;
|
|
|
|
file = new File(sPath);
|
|
|
|
// 判断目录或文件是否存在
|
|
|
|
if (!file.exists()) {
|
|
|
|
} else {
|
|
|
|
// 判断是否为文件
|
|
|
|
if (file.isFile()) {
|
|
|
|
deleteFile(sPath);
|
|
|
|
} else { // 为目录时调用删除目录方法
|
|
|
|
deleteDirectory(sPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 删除单个文件
|
|
|
|
*
|
|
|
|
* @param sPath
|
|
|
|
* 被删除文件的文件名 单个文件删除成功返回true,否则返回false
|
|
|
|
*/
|
|
|
|
public static boolean deleteFile(String sPath) {
|
|
|
|
file = new File(sPath);
|
|
|
|
// 路径为文件且不为空则进行删除
|
|
|
|
return file.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 删除目录(文件夹)以及目录下的文件
|
|
|
|
*
|
|
|
|
* @param sPath
|
|
|
|
* 被删除目录的文件路径 目录删除成功返回true,否则返回false
|
|
|
|
*/
|
|
|
|
public static boolean deleteDirectory(String sPath) {
|
|
|
|
// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
|
|
|
|
if (!sPath.endsWith(File.separator)) {
|
|
|
|
sPath = sPath + File.separator;
|
|
|
|
}
|
|
|
|
File dirFile = new File(sPath);
|
|
|
|
// 如果dir对应的文件不存在,或者不是一个目录,则退出
|
|
|
|
if (!dirFile.exists() || !dirFile.isDirectory()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
flag = true;
|
|
|
|
// 删除文件夹下的所有文件(包括子目录)
|
|
|
|
File[] files = dirFile.listFiles();
|
|
|
|
for (File value : files) {
|
|
|
|
// 删除子文件
|
|
|
|
if (value.isFile()) {
|
|
|
|
flag = deleteFile(value.getAbsolutePath());
|
|
|
|
if (!flag) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // 删除子目录
|
|
|
|
else {
|
|
|
|
flag = deleteDirectory(value.getAbsolutePath());
|
|
|
|
if (!flag) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!flag) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 删除当前目录
|
|
|
|
return dirFile.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String fileToBase64(File file) {
|
|
|
|
if (file == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String base64 = null;
|
|
|
|
FileInputStream fin = null;
|
|
|
|
try {
|
|
|
|
fin = new FileInputStream(file);
|
|
|
|
byte[] buff = new byte[fin.available()];
|
|
|
|
fin.read(buff);
|
|
|
|
base64 = Base64.encode(buff);
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
if (fin != null) {
|
|
|
|
try {
|
|
|
|
fin.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return base64;
|
|
|
|
}
|
|
|
|
|
|
|
|
//新增文件上传接口
|
|
|
|
public static String uploadFile(String url, byte[] bytes) {
|
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addBinaryBody("file", bytes);
|
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httpPost.setEntity(multipart);
|
|
|
|
String result = "";
|
|
|
|
try {
|
|
|
|
CloseableHttpResponse response = HttpClients.createDefault().execute(httpPost);
|
|
|
|
InputStream content = response.getEntity().getContent();
|
|
|
|
result = IOUtils.toString(content, "utf-8");
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|