From e61f82c8e3b48255b5bf1e04b40cfae300ae14a8 Mon Sep 17 00:00:00 2001 From: ruibaby Date: Sun, 22 Jul 2018 16:03:06 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cc/ryanc/halo/utils/HaloUtils.java | 32 ------------------- .../admin/AttachmentController.java | 6 ++-- .../web/controller/admin/ThemeController.java | 11 ++++--- 3 files changed, 9 insertions(+), 40 deletions(-) diff --git a/src/main/java/cc/ryanc/halo/utils/HaloUtils.java b/src/main/java/cc/ryanc/halo/utils/HaloUtils.java index 2878bc10b..1202cb6dc 100755 --- a/src/main/java/cc/ryanc/halo/utils/HaloUtils.java +++ b/src/main/java/cc/ryanc/halo/utils/HaloUtils.java @@ -45,12 +45,6 @@ import java.util.*; @Slf4j public class HaloUtils { - private final static Calendar NOW = Calendar.getInstance(); - - public final static String YEAR = NOW.get(Calendar.YEAR) + ""; - - public final static String MONTH = (NOW.get(Calendar.MONTH) + 1) + ""; - private static ArrayList FILE_LIST = new ArrayList<>(); /** @@ -251,32 +245,6 @@ public class HaloUtils { return tpls; } - /** - * 获取文件内容 - * - * @param filePath filePath - * @return String - */ - 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 { - 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; - } - /** * 获取当前时间 * diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/AttachmentController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/AttachmentController.java index e517da395..e36141f3e 100755 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/AttachmentController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/AttachmentController.java @@ -134,7 +134,7 @@ public class AttachmentController { //upload的路径 StringBuffer sbMedia = new StringBuffer("upload/"); //获取当前年月以创建目录,如果没有该目录则创建 - sbMedia.append(HaloUtils.YEAR).append("/").append(HaloUtils.MONTH).append("/"); + sbMedia.append(DateUtil.thisYear()).append("/").append(DateUtil.thisMonth()).append("/"); File mediaPath = new File(basePath.getAbsolutePath(), sbMedia.toString()); if (!mediaPath.exists()) { mediaPath.mkdirs(); @@ -151,8 +151,8 @@ public class AttachmentController { //保存在数据库 Attachment attachment = new Attachment(); attachment.setAttachName(fileName); - attachment.setAttachPath(new StringBuffer("/upload/").append(HaloUtils.YEAR).append("/").append(HaloUtils.MONTH).append("/").append(fileName).toString()); - attachment.setAttachSmallPath(new StringBuffer("/upload/").append(HaloUtils.YEAR).append("/").append(HaloUtils.MONTH).append("/").append(nameWithOutSuffix).append("_small.").append(fileSuffix).toString()); + attachment.setAttachPath(new StringBuffer("/upload/").append(DateUtil.thisYear()).append("/").append(DateUtil.thisMonth()).append("/").append(fileName).toString()); + attachment.setAttachSmallPath(new StringBuffer("/upload/").append(DateUtil.thisYear()).append("/").append(DateUtil.thisMonth()).append("/").append(nameWithOutSuffix).append("_small.").append(fileSuffix).toString()); attachment.setAttachType(file.getContentType()); attachment.setAttachSuffix(new StringBuffer(".").append(fileSuffix).toString()); attachment.setAttachCreated(DateUtil.date()); diff --git a/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java b/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java index 74a170dd8..3cfc8fae3 100755 --- a/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java +++ b/src/main/java/cc/ryanc/halo/web/controller/admin/ThemeController.java @@ -12,6 +12,8 @@ import cc.ryanc.halo.utils.HaloUtils; import cc.ryanc.halo.web.controller.core.BaseController; import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.file.FileReader; +import cn.hutool.core.io.file.FileWriter; import cn.hutool.core.util.ZipUtil; import cn.hutool.extra.servlet.ServletUtil; import lombok.extern.slf4j.Slf4j; @@ -27,8 +29,6 @@ import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.websocket.server.PathParam; import java.io.File; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.List; /** @@ -184,7 +184,8 @@ public class ThemeController extends BaseController { File basePath = new File(ResourceUtils.getURL("classpath:").getPath()); //获取主题路径 File themesPath = new File(basePath.getAbsolutePath(), new StringBuffer("templates/themes/").append(BaseController.THEME).append("/").append(tplName).toString()); - tplContent = HaloUtils.getFileContent(themesPath.getAbsolutePath()); + FileReader fileReader = new FileReader(themesPath); + tplContent = fileReader.readString(); } catch (Exception e) { log.error("获取模板文件错误:{}", e.getMessage()); } @@ -210,8 +211,8 @@ public class ThemeController extends BaseController { File basePath = new File(ResourceUtils.getURL("classpath:").getPath()); //获取主题路径 File tplPath = new File(basePath.getAbsolutePath(), new StringBuffer("templates/themes/").append(BaseController.THEME).append("/").append(tplName).toString()); - byte[] tplContentByte = tplContent.getBytes("UTF-8"); - Files.write(Paths.get(tplPath.getAbsolutePath()), tplContentByte); + FileWriter fileWriter = new FileWriter(tplPath); + fileWriter.write(tplContent); } catch (Exception e) { log.error("模板保存失败:{}", e.getMessage()); return new JsonResult(ResultCode.FAIL.getCode(), "模板保存失败!");