add IP Region for luoo_comment

main
wangqing 10 months ago
parent edf3f59f47
commit 09c617c1fb

@ -62,6 +62,11 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
</dependency>
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
<build>
<finalName>app</finalName>

@ -14,6 +14,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;
@RestController
@ -30,6 +31,10 @@ public class CommentController extends BaseController {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private HttpServletRequest request;
@GetMapping
public Result findAll(){
return Result.success(commentService.findAll());
@ -69,6 +74,7 @@ public class CommentController extends BaseController {
BeanUtils.copyProperties(commentVo, comment);
comment.setUserId(userId);
comment.setNickName(userLoginDto.getNickName());
comment.setLocation(getIpAddr(request));
commentService.save(comment);
return Result.success();
}

@ -31,6 +31,8 @@ public class Comment implements Serializable {
// 状态
private Integer state;
private String location; //归属地
/**
* ID
*/

@ -0,0 +1,31 @@
package com.luoo.comment.util;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
import java.io.InputStream;
public class IpUtil {
private static final String IP2REGION_DB_FILE_PATH = "ip2region.xdb";
private static final String UNKNOW_IP = "未知IP";
private static final Searcher searcher = initSearcher();
private static Searcher initSearcher() {
try (InputStream is = new ClassPathResource(IP2REGION_DB_FILE_PATH).getInputStream();) {
byte[] cBuff = StreamUtils.copyToByteArray(is);// Searcher.loadContent(randomAccessFile);
return Searcher.newWithBuffer(cBuff);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getIpLocation(String ip) {
try {
return searcher.search(ip).split("\\|")[2].replaceFirst("省", "");
} catch (Exception e) {
return UNKNOW_IP;
}
}
}
Loading…
Cancel
Save