1.修复ip2region 不会自动关闭连接问题

pull/217/head
邹伟华 2019-12-05 10:26:03 +08:00
parent 33cdb7fea1
commit 7c5193a93d
1 changed files with 12 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
@ -143,12 +144,13 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* ip
*/
public static String getCityInfo(String ip) {
DbSearcher searcher = null;
try {
String path = "ip2region/ip2region.db";
String name = "ip2region.db";
DbConfig config = new DbConfig();
File file = FileUtil.inputStreamToFile(new ClassPathResource(path).getStream(), name);
DbSearcher searcher = new DbSearcher(config, file.getPath());
searcher = new DbSearcher(config, file.getPath());
Method method;
method = searcher.getClass().getMethod("btreeSearch", String.class);
DataBlock dataBlock;
@ -161,6 +163,14 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
return address.equals(ElAdminConstant.REGION)?"内网IP":address;
} catch (Exception e) {
e.printStackTrace();
}finally {
if(searcher!=null){
try {
searcher.close();
} catch (IOException ignored) {
}
}
}
return "";
}
@ -185,4 +195,4 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
}
return weekDays[w];
}
}
}