🎨 代码优化

pull/33/merge
ruibaby 2018-07-20 13:01:08 +08:00
parent cf3a107ccd
commit 9fdf094b2c
2 changed files with 22 additions and 63 deletions

View File

@ -257,17 +257,22 @@ public class HaloUtils {
* @param filePath filePath * @param filePath filePath
* @return String * @return String
*/ */
public static String getFileContent(String filePath) { public static String getFileContent(String filePath) throws IOException {
File file = new File(filePath); File file = new File(filePath);
Long fileLength = file.length(); Long fileLength = file.length();
byte[] fileContent = new byte[fileLength.intValue()]; byte[] fileContent = new byte[fileLength.intValue()];
FileInputStream inputStream = null;
try { try {
FileInputStream inputStream = new FileInputStream(file); inputStream = new FileInputStream(file);
inputStream.read(fileContent); inputStream.read(fileContent);
inputStream.close(); inputStream.close();
return new String(fileContent, "UTF-8"); return new String(fileContent, "UTF-8");
} catch (Exception e) { } catch (Exception e) {
log.error("读取模板文件错误:", e.getMessage()); log.error("读取模板文件错误:", e.getMessage());
} finally {
if (null != inputStream) {
inputStream.close();
}
} }
return null; return null;
} }
@ -288,59 +293,6 @@ public class HaloUtils {
return Instant.ofEpochSecond(unixTime).atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(format)); 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 filePath
* @param fileName * @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 { try {
File file = new File(filePath); File file = new File(filePath);
if (!file.exists()) { if (!file.exists()) {
file.mkdirs(); file.mkdirs();
} }
//true = append file fileWriter = new FileWriter(file.getAbsoluteFile() + "/" + fileName, true);
FileWriter fileWritter = new FileWriter(file.getAbsoluteFile() + "/" + fileName, true); bufferedWriter = new BufferedWriter(fileWriter);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter); bufferedWriter.write(data);
bufferWritter.write(data);
bufferWritter.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (null != bufferedWriter) {
bufferedWriter.close();
}
if (null != fileWriter) {
fileWriter.close();
}
} }
} }

View File

@ -30,7 +30,7 @@ public class ApiArchivesController {
@GetMapping(value = "/year") @GetMapping(value = "/year")
public JsonResult archivesYear() { public JsonResult archivesYear() {
List<Archive> archives = postService.findPostGroupByYear(); 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); return new JsonResult(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMsg(), archives);
} else { } else {
return new JsonResult(ResponseStatus.EMPTY.getCode(), ResponseStatus.EMPTY.getMsg()); return new JsonResult(ResponseStatus.EMPTY.getCode(), ResponseStatus.EMPTY.getMsg());
@ -45,7 +45,7 @@ public class ApiArchivesController {
@GetMapping(value = "/year/month") @GetMapping(value = "/year/month")
public JsonResult archivesYearAndMonth() { public JsonResult archivesYearAndMonth() {
List<Archive> archives = postService.findPostGroupByYearAndMonth(); 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); return new JsonResult(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMsg(), archives);
} else { } else {
return new JsonResult(ResponseStatus.EMPTY.getCode(), ResponseStatus.EMPTY.getMsg()); return new JsonResult(ResponseStatus.EMPTY.getCode(), ResponseStatus.EMPTY.getMsg());