|
@@ -1,6 +1,7 @@
|
|
|
package com.qmth.themis.common.util;
|
|
|
|
|
|
import com.qmth.themis.common.exception.BusinessException;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.lionsoul.ip2region.xdb.Searcher;
|
|
|
import org.slf4j.Logger;
|
|
@@ -9,8 +10,7 @@ import org.slf4j.LoggerFactory;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
-import java.net.URL;
|
|
|
-import java.util.Enumeration;
|
|
|
+import java.io.InputStream;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
public class IpUtil {
|
|
@@ -120,24 +120,22 @@ public class IpUtil {
|
|
|
/**
|
|
|
* 获取全局searcher
|
|
|
*
|
|
|
+ * @param path
|
|
|
* @return
|
|
|
*/
|
|
|
- public static Searcher getSearcher() {
|
|
|
- String path = null;
|
|
|
- try {
|
|
|
- Enumeration<URL> urls = IpUtil.class.getClassLoader().getResources("ip2region.xdb");
|
|
|
- int i = 1;
|
|
|
- while (urls.hasMoreElements()) {
|
|
|
- URL url = urls.nextElement();
|
|
|
- path = url.getPath();
|
|
|
- log.info("===={}, {}", i++, url.getPath());
|
|
|
+ public static Searcher getSearcher(String path) {
|
|
|
+ File file = null;
|
|
|
+ InputStream inputStream = IpUtil.class.getClassLoader().getResourceAsStream("ip2region.xdb");
|
|
|
+ if (Objects.isNull(inputStream)) {
|
|
|
+ log.info("inputstream is null");
|
|
|
+ } else {
|
|
|
+ file = new File(path + File.separator + "ip2region.xdb");
|
|
|
+ try {
|
|
|
+ FileUtils.copyInputStreamToFile(inputStream, file);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
}
|
|
|
- URL url = IpUtil.class.getClassLoader().getResource("ip2region.xdb");
|
|
|
- log.info("ip2region url:{},file:{}", url, url.getFile());
|
|
|
- File file = Objects.nonNull(path) ? new File(path) : null;
|
|
|
if (Objects.isNull(file)) {
|
|
|
return null;
|
|
|
}
|