mirror of https://github.com/halo-dev/halo
🎨 代码优化
parent
cf3a107ccd
commit
9fdf094b2c
|
@ -257,17 +257,22 @@ public class HaloUtils {
|
|||
* @param filePath filePath
|
||||
* @return String
|
||||
*/
|
||||
public static String getFileContent(String filePath) {
|
||||
public static String getFileContent(String filePath) throws IOException {
|
||||
File file = new File(filePath);
|
||||
Long fileLength = file.length();
|
||||
byte[] fileContent = new byte[fileLength.intValue()];
|
||||
FileInputStream inputStream = null;
|
||||
try {
|
||||
FileInputStream inputStream = new FileInputStream(file);
|
||||
inputStream = new FileInputStream(file);
|
||||
inputStream.read(fileContent);
|
||||
inputStream.close();
|
||||
return new String(fileContent, "UTF-8");
|
||||
} catch (Exception e) {
|
||||
log.error("读取模板文件错误:", e.getMessage());
|
||||
} finally {
|
||||
if (null != inputStream) {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -288,59 +293,6 @@ public class HaloUtils {
|
|||
return Instant.ofEpochSecond(unixTime).atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(format));
|
||||
}
|
||||
|
||||
/**
|
||||
* 备份数据库
|
||||
*
|
||||
* @param hostIp ip
|
||||
* @param userName 用户名
|
||||
* @param password password
|
||||
* @param savePath 保存路径
|
||||
* @param fileName 文件名
|
||||
* @param databaseName 数据库名
|
||||
* @return boolean
|
||||
* @throws InterruptedException InterruptedException
|
||||
*/
|
||||
public static boolean exportDatabase(String hostIp, String userName, String password, String savePath, String fileName, String databaseName) throws InterruptedException {
|
||||
File saveFile = new File(savePath);
|
||||
if (!saveFile.exists()) {
|
||||
saveFile.mkdirs();
|
||||
}
|
||||
if (!savePath.endsWith(File.separator)) {
|
||||
savePath = savePath + File.separator;
|
||||
}
|
||||
|
||||
PrintWriter printWriter = null;
|
||||
BufferedReader bufferedReader = null;
|
||||
try {
|
||||
printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf-8"));
|
||||
Process process = Runtime.getRuntime().exec(" mysqldump -h" + hostIp + " -u" + userName + " -p" + password + " --set-charset=UTF8 " + databaseName);
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf-8");
|
||||
bufferedReader = new BufferedReader(inputStreamReader);
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
printWriter.println(line);
|
||||
}
|
||||
printWriter.flush();
|
||||
if (process.waitFor() == 0) {
|
||||
return true;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (bufferedReader != null) {
|
||||
bufferedReader.close();
|
||||
}
|
||||
if (printWriter != null) {
|
||||
printWriter.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出为文件
|
||||
*
|
||||
|
@ -348,19 +300,26 @@ public class HaloUtils {
|
|||
* @param filePath 保存路径
|
||||
* @param fileName 文件名
|
||||
*/
|
||||
public static void postToFile(String data, String filePath, String fileName) {
|
||||
public static void postToFile(String data, String filePath, String fileName) throws IOException {
|
||||
FileWriter fileWriter = null;
|
||||
BufferedWriter bufferedWriter = null;
|
||||
try {
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
file.mkdirs();
|
||||
}
|
||||
//true = append file
|
||||
FileWriter fileWritter = new FileWriter(file.getAbsoluteFile() + "/" + fileName, true);
|
||||
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
|
||||
bufferWritter.write(data);
|
||||
bufferWritter.close();
|
||||
fileWriter = new FileWriter(file.getAbsoluteFile() + "/" + fileName, true);
|
||||
bufferedWriter = new BufferedWriter(fileWriter);
|
||||
bufferedWriter.write(data);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (null != bufferedWriter) {
|
||||
bufferedWriter.close();
|
||||
}
|
||||
if (null != fileWriter) {
|
||||
fileWriter.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ public class ApiArchivesController {
|
|||
@GetMapping(value = "/year")
|
||||
public JsonResult archivesYear() {
|
||||
List<Archive> archives = postService.findPostGroupByYear();
|
||||
if (null != archives || archives.size() > 0) {
|
||||
if (null != archives && archives.size() > 0) {
|
||||
return new JsonResult(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMsg(), archives);
|
||||
} else {
|
||||
return new JsonResult(ResponseStatus.EMPTY.getCode(), ResponseStatus.EMPTY.getMsg());
|
||||
|
@ -45,7 +45,7 @@ public class ApiArchivesController {
|
|||
@GetMapping(value = "/year/month")
|
||||
public JsonResult archivesYearAndMonth() {
|
||||
List<Archive> archives = postService.findPostGroupByYearAndMonth();
|
||||
if (null != archives || archives.size() > 0) {
|
||||
if (null != archives && archives.size() > 0) {
|
||||
return new JsonResult(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMsg(), archives);
|
||||
} else {
|
||||
return new JsonResult(ResponseStatus.EMPTY.getCode(), ResponseStatus.EMPTY.getMsg());
|
||||
|
|
Loading…
Reference in New Issue