1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cn.wisenergy.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.*;
import java.util.Properties;
@Slf4j
public class FileUtil {
/**
* @param filePath
* @return
* @throws IOException
* @Title: readProperties
* @Description: 读取prop配置文件获取Properties对象
*/
public static Properties readProperties(String filePath) throws IOException {
// 创建Properties文件
Properties prop = new Properties();
// 获取流
InputStream input = FileUtil.class.getClassLoader().getResourceAsStream(filePath);
// 导入流
prop.load(input);
return prop;
}
/**
* @param filePath 文件的路径
* @return
* @Title: readJsonFile
* @Description: 将JSON文件读取为JSON字符串
*/
public static String readJsonFile(String filePath) {
String laststr = "";
File file = new File(filePath);// 打开文件
BufferedReader reader = null;
try {
FileInputStream in = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));// 读取文件
String tempString = null;
while ((tempString = reader.readLine()) != null) {
laststr = laststr + tempString;
}
reader.close();
} catch (IOException e) {
log.error(e.getMessage(),e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException el) {
log.error(el.getMessage(),el);
}
}
}
return laststr;
}
/**
* 将URL远程路径文件转成InputStream返回
*
* @param url
* 远程地址
* @return InputStream
*/
public static InputStream remotePathToStream(String url) {
InputStream inputStream = null;
try {
CloseableHttpClient httpsClient = HttpUtil.createSSLClientDefault();
HttpGet get = new HttpGet(url);
HttpResponse response = httpsClient.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
inputStream = response.getEntity().getContent();
}
} catch (Exception e) {
ExceptionUtils.getStackTrace(e);
}
return inputStream;
}
}