Skip to content

Commit 202c47b

Browse files
committed
将FileUtil合并至IOUtil
1 parent bb2070f commit 202c47b

5 files changed

Lines changed: 60 additions & 75 deletions

File tree

src/main/java/org/eh/core/http/EHHttpHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.eh.core.common.ReturnType;
1515
import org.eh.core.model.ResultInfo;
1616
import org.eh.core.util.FileUploadContentAnalysis;
17-
import org.eh.core.util.FileUtil;
1817
import org.eh.core.util.IOUtil;
1918
import org.eh.core.util.StringUtil;
2019
import org.eh.core.web.controller.Controller;
@@ -40,7 +39,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
4039
// 根据后缀判断是否是静态资源
4140
String suffix = path.substring(path.lastIndexOf("."), path.length());
4241
if (Constants.STATIC_SUFFIXS.contains(suffix)) {
43-
byte[] bytes = FileUtil.readFileByBytes(this.getClass().getResource("/").getPath()
42+
byte[] bytes = IOUtil.readFileByBytes(this.getClass().getResource("/").getPath()
4443
+ "static" + path);
4544
responseStaticToClient(httpExchange, 200, bytes);
4645
return;

src/main/java/org/eh/core/util/FileUtil.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/main/java/org/eh/core/util/IOUtil.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.eh.core.util;
22

33
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileInputStream;
46
import java.io.IOException;
57
import java.io.InputStream;
68
import java.io.InputStreamReader;
9+
import java.nio.ByteBuffer;
10+
import java.nio.channels.FileChannel;
711

812
import org.apache.commons.logging.Log;
913
import org.apache.commons.logging.LogFactory;
@@ -31,4 +35,55 @@ public static String getRequestContent(InputStream inputStream) {
3135
}
3236
return sb.toString();
3337
}
38+
39+
public static boolean isExist(String path) {
40+
File file = new File(path);
41+
return file.exists();
42+
}
43+
44+
public static String readFile(String path) {
45+
if (!isExist(path)) {
46+
return "";
47+
}
48+
FileInputStream fis = null;
49+
StringBuilder sb = new StringBuilder();
50+
try {
51+
fis = new FileInputStream(path);
52+
FileChannel c = fis.getChannel();
53+
ByteBuffer bc = ByteBuffer.allocate(1024);
54+
int i = c.read(bc);
55+
while (i != -1) {
56+
sb.append(new String(bc.array()));
57+
bc.clear();
58+
i = c.read(bc);
59+
}
60+
c.close();
61+
fis.close();
62+
} catch (Exception e) {
63+
log.error("读取模板文件失败:", e);
64+
}
65+
return sb.toString();
66+
}
67+
68+
public static byte[] readFileByBytes(String path) {
69+
if (!isExist(path)) {
70+
return "".getBytes();
71+
}
72+
byte[] bytes = null;
73+
FileInputStream fis = null;
74+
try {
75+
fis = new FileInputStream(path);
76+
FileChannel c = fis.getChannel();
77+
ByteBuffer bc = ByteBuffer.allocate((int) c.size());
78+
int i = c.read(bc);
79+
if (i != -1) {
80+
bytes = bc.array();
81+
}
82+
c.close();
83+
fis.close();
84+
} catch (Exception e) {
85+
log.error("读取资源文件失败:", e);
86+
}
87+
return bytes;
88+
}
3489
}

src/main/java/org/eh/core/web/view/ViewHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import org.eh.core.common.Constants;
66
import org.eh.core.model.ResultInfo;
7-
import org.eh.core.util.FileUtil;
7+
import org.eh.core.util.IOUtil;
88
import org.eh.core.util.StringUtil;
99
import org.eh.core.util.VelocityUtil;
1010

@@ -23,8 +23,8 @@ public String processView(ResultInfo resultInfo) {
2323
// 获取路径
2424
String path = analysisViewPath(resultInfo.getView());
2525
String content = "";
26-
if (FileUtil.isExist(path)) {
27-
content = FileUtil.readFile(path);
26+
if (IOUtil.isExist(path)) {
27+
content = IOUtil.readFile(path);
2828
}
2929

3030
if (StringUtil.isEmpty(content)) {

src/test/java/org/eh/core/util/FileUtilTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class FileUtilTest {
1313
public void TestRead() {
1414
String path = this.getClass().getResource("/").getPath()
1515
+ "org/eh/web/view/test/myinfo.page";
16-
String content = FileUtil.readFile(path);
16+
String content = IOUtil.readFile(path);
1717
System.out.println(content);
1818
}
1919

0 commit comments

Comments
 (0)