今天发现文章评论数与实际不符

将近晚上十点看了下博客,结果发现Google镜像站搜集的评论数与实际不符。感觉有点郁闷。翻了下代码(LyController)。
    @POST("/article/ly") public void articleLy(@Param Ly ly, HttpServletResponse response) {
        Article article = lyService.get(Article.class, ly.getArticleId());
        if(ObjectUtil.isNotNull(article)) {
            Long assess = article.getAssess();
            article = new Article();
            article.setId(ly.getArticleId());
            article.setAssess(assess + 1);
            article.setModifyDatetime(UserManage.getReceiveDatetime());
            lyService.update(article);
            ly.setWebappId(UserManage.getWebappId());
            ly.setCreateDatetime(UserManage.getReceiveDatetime());
            ly.setModifyDatetime(UserManage.getReceiveDatetime()); 
            ly.setImg(getImg());
            String ip = WebUtil.getIp();
            ly.setIp(ip);
            ly.setIpName(ipService.getName(IpUtil.toLong(ip)));
            lyService.save(ly);
        }
        WebUtil.redirect("/article/" + ly.getArticleId());
    }
看了看,我的事务是放在Service,错误应该是发生在这里前半部分更新文章评论数成功,后面插入留言出错。直接上Tomcat日志
Aug 17, 2015 9:46:44 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet default threw exception
java.lang.NumberFormatException: For input string: "184, 106"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Long.parseLong(Long.java:441)
	at java.lang.Long.valueOf(Long.java:540)
	at org.jiucheng.util.IpUtil.toLong(IpUtil.java:11)
	at in.wenwen.controller.LyController.articleLy(LyController.java:60)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.jiucheng.web.handler.DefaultHandler.invoke(DefaultHandler.java:34)
	at org.jiucheng.web.filter.DispatcherFilter.doFilter(DispatcherFilter.java:56)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.jiucheng.web.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:34)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:620)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
	at java.lang.Thread.run(Thread.java:745)
问题出在IP地址转换成数字这里,”184, 106“这样的IP没见过。 WebUtil.getIp()如下:
    /**
     * 获取IP
     */
    public static String getIp() {
        HttpServletRequest request = getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
IpUtil代码如下:
package org.jiucheng.util;

public class IpUtil {
    
    private static final long[] MASK = {0xFF000000,0x00FF0000,0x0000FF00,0x000000FF};
    
    public static long toLong(String ip) {
        String[] is = ip.split("\\.");
        long rs = 0L;
        for(int i = 0, len = is.length; i < len; i ++) { rs += Math.pow(256,(len - i - 1)) * Long.valueOf(is[i]); } return rs; } public static String frLong(long ip) { StringBuilder sb = new StringBuilder(); for(int i = 0,len = MASK.length; i < len; i ++) { sb.append(Long.toString((ip & MASK[i]) >> (len - i - 1) * 8)).append(".");
        }
        return sb.substring(0, sb.length() - 1);
    }
}
117

评论

  1. 嗯 看不懂,,好牛X的感觉。。。

    1. @Mrxn 你才是牛人,多帮测测。

  2. 我的评论没有记录IP这些。。。统计直接用触发器呗。文章里面弄个统计字段。

    1. @强哥 文章有统计评论个数的,只是和实际统计不同。IP来路用的纯真

      1. @旧城 我擦、弄个评论通知什么的啊。要不然别人怎么知道?

  3. 几乎不去关注这些。

    1. @夏日博客 程序猿,严谨点还是很有必要的。

      1. @旧城 三三四四

  4. 牛人

发表评论

电子邮件地址不会被公开。 必填项已用*标注

点击更换