修复无法获取blog-resource.jar包资源的问题

This commit is contained in:
禾几海
2021-10-03 11:14:25 +08:00
parent ab13a1713f
commit c6a5de6335

View File

@@ -1,10 +1,12 @@
package cn.celess.visitor.util;
import com.google.common.io.Files;
import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;
import java.io.File;
@@ -24,20 +26,18 @@ public class AddressUtil {
File file;
try {
//db
String dbPath = AddressUtil.class.getResource("/ip2region/ip2region.db").getPath();
file = new File(dbPath);
if (!file.exists()) {
String tmpDir = System.getProperties().getProperty("java.io.tmpdir");
dbPath = tmpDir + "ip.db";
file = new File(dbPath);
FileCopyUtils.copy(Objects.requireNonNull(AddressUtil.class.getClassLoader().getResourceAsStream("classpath:ip2region/ip2region.db")), new FileOutputStream(file));
}
ClassPathResource resource = new ClassPathResource("ip2region/ip2region.db");
File tempDir = Files.createTempDir();
tempDir.deleteOnExit();
file = new File(tempDir, "ip.db");
FileCopyUtils.copy(Objects.requireNonNull(resource.getInputStream()), new FileOutputStream(file));
//查询算法
//B-tree
int algorithm = DbSearcher.BTREE_ALGORITHM;
try {
DbConfig config = new DbConfig();
DbSearcher searcher = new DbSearcher(config, dbPath);
DbSearcher searcher = new DbSearcher(config, file.getAbsolutePath());
Method method = null;
switch (algorithm) {
case DbSearcher.BTREE_ALGORITHM: