Skip to content

Commit f28efa9

Browse files
committed
去掉使用ip2region库;关闭文件没有关闭的流
1 parent fe597f9 commit f28efa9

File tree

12 files changed

+74
-43
lines changed

12 files changed

+74
-43
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,22 @@ JavaLib,是一个Java开发基础工具类库,对项目开发中常用的工
4242
第一步,添加远程仓库:
4343

4444
```xml
45-
<repositories>
46-
<repository>
47-
<id>jitpack.io</id>
48-
<url>https://jitpack.io</url>
49-
</repository>
50-
</repositories>
45+
<repositories>
46+
<repository>
47+
<id>jitpack.io</id>
48+
<url>https://jitpack.io</url>
49+
</repository>
50+
</repositories>
5151
```
5252

5353
第二步,添加依赖:
5454

5555
```xml
56-
<dependency>
57-
<groupId>com.github.iutil</groupId>
58-
<artifactId>JavaLib</artifactId>
59-
<version>1.0.1</version>
60-
</dependency>
56+
<dependency>
57+
<groupId>com.github.iutil</groupId>
58+
<artifactId>JavaLib</artifactId>
59+
<version>1.0.2</version>
60+
</dependency>
6161
```
6262

6363
## Wiki

bak/文件读写.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
```java
2+
/**
3+
* 根据文件路径读取文件内容
4+
* [仅做参考]
5+
* @param path 文件路径
6+
* @return 文件内容
7+
* @throws IOException 读取失败
8+
*/
9+
private String read(String path) throws IOException {
10+
StringBuffer sb = new StringBuffer();
11+
String tempStr;
12+
File file = new File(path);
13+
// 如果文件不存在,读取失败
14+
if (!file.exists()) throw new FileNotFoundException();
15+
FileInputStream fis = new FileInputStream(file);
16+
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
17+
while ((tempStr = br.readLine()) != null) sb.append(tempStr);
18+
br.close();
19+
return sb.toString();
20+
}
21+
```

doc/DOC-V1.0.2.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868

6969
`Instant parseDate(String source)`: 日期字符串转化为Instant
7070

71+
`Instant parseDateTime(String source, String format)`: 将 时间字符串 转为 Instant
72+
7173
## 进制转换工具类(HexUtils)
7274

7375
`String _2_16(byte[] bytes)`: 二进制转十六进制

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@
8989
</dependency>
9090

9191
<!-- ip地址 -->
92-
<dependency>
92+
<!--<dependency>
9393
<groupId>org.lionsoul</groupId>
9494
<artifactId>ip2region</artifactId>
9595
<version>1.7.2</version>
96-
</dependency>
96+
</dependency>-->
9797
</dependencies>
9898

9999
<parent>

src/main/java/net/iutil/javalib/util/DateTimeUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,16 @@ public static Instant parseDate(String source) {
579579
return date.atStartOfDay(ZoneId.systemDefault()).toInstant();
580580
}
581581

582+
/**
583+
* 将 时间字符串 转为 Instant
584+
* @param source 时间字符串
585+
* @param format 格式
586+
* @return Instant
587+
*/
588+
public static Instant parseDateTime(String source, String format) {
589+
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
590+
LocalDateTime dateTime = LocalDateTime.parse(source, dateTimeFormatter);
591+
return dateTime.atZone(ZoneOffset.systemDefault()).toInstant();
592+
}
593+
582594
}

src/main/java/net/iutil/javalib/util/FileUtils.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,4 @@ public static void write(String path, String str, boolean isNewMode) throws IOEx
2828
out.close();
2929
}
3030

31-
/**
32-
* 根据文件路径读取文件内容
33-
* [仅做参考]
34-
* @param path 文件路径
35-
* @return 文件内容
36-
* @throws IOException 读取失败
37-
*/
38-
private String read(String path) throws IOException {
39-
StringBuffer sb = new StringBuffer();
40-
String tempStr;
41-
File file = new File(path);
42-
// 如果文件不存在,读取失败
43-
if (!file.exists()) throw new FileNotFoundException();
44-
FileInputStream fis = new FileInputStream(file);
45-
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
46-
while ((tempStr = br.readLine()) != null) sb.append(tempStr);
47-
return sb.toString();
48-
}
49-
5031
}

src/main/java/net/iutil/javalib/util/IpUtils.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
77
import okhttp3.OkHttpClient;
88
import okhttp3.Request;
99
import okhttp3.Response;
10-
import org.lionsoul.ip2region.*;
1110

12-
import java.io.File;
13-
import java.io.FileNotFoundException;
1411
import java.io.IOException;
15-
import java.lang.reflect.InvocationTargetException;
16-
import java.lang.reflect.Method;
12+
13+
//import org.lionsoul.ip2region.*;
1714

1815
/**
1916
* @author Wenyi Feng
@@ -89,7 +86,7 @@ private static String getIpInfoByIp(String url) throws IOException {
8986
}
9087

9188
// 有问题 还在测试
92-
public static String getCityInfo(int algorithm, String ip) throws
89+
/*public static String getCityInfo(int algorithm, String ip) throws
9390
DbMakerConfigException,
9491
FileNotFoundException,
9592
NoSuchMethodException,
@@ -117,6 +114,6 @@ public static String getCityInfo(int algorithm, String ip) throws
117114
}
118115
DataBlock dataBlock = (DataBlock) method.invoke(searcher, ip);
119116
return dataBlock.getRegion();
120-
}
117+
}*/
121118

122119
}

src/main/java/net/iutil/javalib/util/RequestUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public static String getPostData(HttpServletRequest request) throws IOException
8686
sb.append(line);
8787
}
8888
//return URLDecoder.decode(sb.toString(), Constant.DEFAULT_CHATSET);
89+
br.close();
8990
return sb.toString();
9091
}
9192
}

src/main/resources/ip2region.db

-2.6 MB
Binary file not shown.

src/test/java/net/iutil/javalib/DateTimeUtilsTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,18 @@ public void instantFormat() {
1919
PrintUtils.info(format);
2020
}
2121

22+
@Test
23+
public void date2Instant() {
24+
String date = "2019-07-09";
25+
Instant instant = DateTimeUtils.parseDate(date);
26+
PrintUtils.info(instant);
27+
}
28+
29+
@Test
30+
public void dateTime2Instant() {
31+
String dateTime = "2019-07-09 18:00:00";
32+
Instant instant = DateTimeUtils.parseDateTime(dateTime, "yyyy-MM-dd HH:mm:ss");
33+
PrintUtils.info(instant);
34+
}
35+
2236
}

0 commit comments

Comments
 (0)