文档章节

代码保存

五大三粗
 五大三粗
发布于 2015/04/15 14:44
字数 948
阅读 81
收藏 3

获取客户端真实IP

package org.j4.utils;

import java.util.regex.Matcher;

public class WebUtils {
	/**
	 * 取得客户端真实ip
	 */
	public static String getIpAddr() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).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;
	}

	private final static String IE10 = "MSIE 10.0";
	private final static String IE9 = "MSIE 9.0";
	private final static String IE8 = "MSIE 8.0";
	private final static String IE7 = "MSIE 7.0";
	private final static String IE6 = "MSIE 6.0";
	private final static String MAXTHON = "Maxthon";
	private final static String QQ = "QQBrowser";
	private final static String GREEN = "GreenBrowser";
	private final static String SE360 = "360SE";
	private final static String FIREFOX = "Firefox";
	private final static String OPERA = "Opera";
	private final static String CHROME = "Chrome";
	private final static String SAFARI = "Safari";
	private final static String OTHER = "其它";

	public static String getBrowse() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		String userAgent = request.getHeader("USER-AGENT");
		if (regex(OPERA, userAgent))
			return OPERA;
		if (regex(CHROME, userAgent))
			return CHROME;
		if (regex(FIREFOX, userAgent))
			return FIREFOX;
		if (regex(SAFARI, userAgent))
			return SAFARI;
		if (regex(SE360, userAgent))
			return SE360;
		if (regex(GREEN, userAgent))
			return GREEN;
		if (regex(QQ, userAgent))
			return QQ;
		if (regex(MAXTHON, userAgent))
			return MAXTHON;
		if (regex(IE10, userAgent))
			return IE10;
		if (regex(IE9, userAgent))
			return IE9;
		if (regex(IE8, userAgent))
			return IE8;
		if (regex(IE7, userAgent))
			return IE7;
		if (regex(IE6, userAgent))
			return IE6;
		return OTHER;
	}

	public static boolean regex(String regex, String str) {
		Pattern p = Pattern.compile(regex, Pattern.MULTILINE);
		Matcher m = p.matcher(str);
		return m.find();
	}
}


金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出。


package test.format;
import java.text.NumberFormat;
import java.util.HashMap;
public class SimpleMoneyFormat {
  public static final String EMPTY = "";
  public static final String ZERO = "零";
  public static final String ONE = "壹";
  public static final String TWO = "贰";
  public static final String THREE = "叁";
  public static final String FOUR = "肆";
  public static final String FIVE = "伍";
  public static final String SIX = "陆";
  public static final String SEVEN = "柒";
  public static final String EIGHT = "捌";
  public static final String NINE = "玖";
  public static final String TEN = "拾";
  public static final String HUNDRED = "佰";
  public static final String THOUSAND = "仟";
  public static final String TEN_THOUSAND = "万";
  public static final String HUNDRED_MILLION = "亿";
  public static final String YUAN = "元";
  public static final String JIAO = "角";
  public static final String FEN = "分";
  public static final String DOT = ".";

  private static SimpleMoneyFormat formatter = null;
  private HashMap chineseNumberMap = new HashMap();
  private HashMap chineseMoneyPattern = new HashMap();
  private NumberFormat numberFormat = NumberFormat.getInstance();

  private SimpleMoneyFormat() {
    numberFormat.setMaximumFractionDigits(4);
    numberFormat.setMinimumFractionDigits(2);
    numberFormat.setGroupingUsed(false);

    chineseNumberMap.put("0", ZERO);
    chineseNumberMap.put("1", ONE);
    chineseNumberMap.put("2", TWO);
    chineseNumberMap.put("3", THREE);
    chineseNumberMap.put("4", FOUR);
    chineseNumberMap.put("5", FIVE);
    chineseNumberMap.put("6", SIX);
    chineseNumberMap.put("7", SEVEN);
    chineseNumberMap.put("8", EIGHT);
    chineseNumberMap.put("9", NINE);
    chineseNumberMap.put(DOT, DOT);

    chineseMoneyPattern.put("1", TEN);
    chineseMoneyPattern.put("2", HUNDRED);
    chineseMoneyPattern.put("3", THOUSAND);
    chineseMoneyPattern.put("4", TEN_THOUSAND);
    chineseMoneyPattern.put("5", TEN);
    chineseMoneyPattern.put("6", HUNDRED);
    chineseMoneyPattern.put("7", THOUSAND);
    chineseMoneyPattern.put("8", HUNDRED_MILLION);
  }

  public static SimpleMoneyFormat getInstance() {
    if (formatter == null)
      formatter = new SimpleMoneyFormat();
    return formatter;
  }

  public String format(String moneyStr) {
    checkPrecision(moneyStr);
    String result;
    result = convertToChineseNumber(moneyStr);
    result = addUnitsToChineseMoneyString(result);
    return result;
  }

  public String format(double moneyDouble) {
    return format(numberFormat.format(moneyDouble));
  }

  public String format(int moneyInt) {
    return format(numberFormat.format(moneyInt));
  }

  public String format(long moneyLong) {
    return format(numberFormat.format(moneyLong));
  }

  public String format(Number moneyNum) {
    return format(numberFormat.format(moneyNum));
  }

  private String convertToChineseNumber(String moneyStr) {
    String result;
    StringBuffer cMoneyStringBuffer = new StringBuffer();
    for (int i = 0; i < moneyStr.length(); i++) {
      cMoneyStringBuffer.append(chineseNumberMap.get(moneyStr.substring(i, i + 1)));
    }
    //拾佰仟万亿等都是汉字里面才有的单位,加上它们
    int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
    int moneyPatternCursor = 1;
    for (int i = indexOfDot - 1; i > 0; i--) {
      cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY + moneyPatternCursor));
      moneyPatternCursor = moneyPatternCursor == 8 ? 1 : moneyPatternCursor + 1;
    }

    String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer.indexOf("."));
    cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("."), cMoneyStringBuffer.length());
    while (cMoneyStringBuffer.indexOf("零拾") != -1) {
      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零拾"), cMoneyStringBuffer.indexOf("零拾") + 2, ZERO);
    }
    while (cMoneyStringBuffer.indexOf("零佰") != -1) {
      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零佰"), cMoneyStringBuffer.indexOf("零佰") + 2, ZERO);
    }
    while (cMoneyStringBuffer.indexOf("零仟") != -1) {
      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零仟"), cMoneyStringBuffer.indexOf("零仟") + 2, ZERO);
    }
    while (cMoneyStringBuffer.indexOf("零万") != -1) {
      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零万"), cMoneyStringBuffer.indexOf("零万") + 2, TEN_THOUSAND);
    }
    while (cMoneyStringBuffer.indexOf("零亿") != -1) {
      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零亿"), cMoneyStringBuffer.indexOf("零亿") + 2, HUNDRED_MILLION);
    }
    while (cMoneyStringBuffer.indexOf("零零") != -1) {
      cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零零"), cMoneyStringBuffer.indexOf("零零") + 2, ZERO);
    }
    if (cMoneyStringBuffer.lastIndexOf(ZERO) == cMoneyStringBuffer.length() - 1)
      cMoneyStringBuffer.delete(cMoneyStringBuffer.length() - 1, cMoneyStringBuffer.length());
    cMoneyStringBuffer.append(fractionPart);

    result = cMoneyStringBuffer.toString();
    return result;
  }


  private String addUnitsToChineseMoneyString(String moneyStr) {
    String result;
    StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr);
    int indexOfDot = cMoneyStringBuffer.indexOf(DOT);
    cMoneyStringBuffer.replace(indexOfDot, indexOfDot + 1, YUAN);



现在输入n个数字,以逗号,分开;然后可选择升或者降序排序;按提交键就在另一页面显示按什么排序,结果为,提供reset
import java.util.*;
public class bycomma{
 public static String[] splitStringByComma(String source){
   if(source==null||source.trim().equals(""))
    return null;
   StringTokenizer commaToker = new StringTokenizer(source,",");
   String[] result = new String[commaToker.countTokens()];
   int i=0;
   while(commaToker.hasMoreTokens()){
    result[i] = commaToker.nextToken();
    i++;
   }
   return result;
 }
 public static void main(String args[]){
 String[] s = splitStringByComma("5,8,7,4,3,9,1");
 int[] ii = new int[s.length];
 for(int i = 0;i<s.length;i++){
 ii[i] =Integer.parseInt(s[i]);
 }
    Arrays.sort(ii);
    //asc
    for(int i=0;i<s.length;i++){
    System.out.println(ii[i]);
    }
    //desc
    for(int i=(s.length-1);i>=0;i--){
    System.out.println(ii[i]);
    }
 }
}




© 著作权归作者所有

上一篇: 网络协议总结
下一篇: linux学习笔记
五大三粗
粉丝 163
博文 2293
码字总数 4767276
作品 0
广州
程序员
私信 提问
加载中

评论(1)

Neoman
Neoman
这个获取真实IP的代码是错误的,却到处传播
SaveDataPage和SaveFilePage的区以别及结合使用

一.SaveFilePage和SaveDataPage的区别: SaveFilePage:指定的页面可接受二进制文件流、Form域,直接保存修改后的文件。 SaveDataPage:指定的页面可接收页面提交的Word数据区域、Excel表格数据...

山里的红杏
2018/09/10
4
0
Spark的RDD检查点实现分析

概述在《深入理解Spark:核心思想与源码分析》一书中只是简单介绍了下RDD的checkpoint,对本书是个遗憾。所以此文的目的旨在查漏补缺,完善本书的内容。Spark的RDD执行完成之后会保存检查点,...

beliefer
2016/05/26
0
0
MooTools教程(2):DOM选择器

如果你还没有准备好,请先阅读上一篇《Mootools 1.2教程(1)——MooTools介绍》。我们讲了怎么引用MooTools 1.2以及怎么在domready里面调用你的脚本。 今天开始本系列教程的第2讲。在这一讲中...

halfcoder
2010/12/12
874
0
55. (android开发)数据存储之SharePreferences

SharePreferences使用键值存储方式,每条数据都包含 key 和 value。 每个应用都会有一个默认的 SharePreferences 文件,可以在这里保存键值。也可以指定文件名进行保存。 PreferenceManager....

厚土火焰山
2017/12/30
0
0
iOS开发技巧(系列七:图片保存到本地相册)

之前看了一些开源的代码,里面有一个功能,就是将图片下载到相册,仔细看了其中的代码,只有很简单的一句话,并且保存过后,还可以判断是否保存成功。 如下代码所示, 点击按钮,将self.imag...

召唤攻城狮
2014/05/04
18.1K
0

没有更多内容

加载失败,请刷新页面

加载更多

最简单的获取相机拍照的图片

  import android.content.Intent;import android.graphics.Bitmap;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import andr......

MrLins
21分钟前
2
0
说好不哭!数据可视化深度干货,前端开发下一个涨薪点在这里~

随着互联网在各行各业的影响不断深入,数据规模越来越大,各企业也越来越重视数据的价值。作为一家专业的数据智能公司,个推从消息推送服务起家,经过多年的持续耕耘,积累沉淀了海量数据,在...

个推
23分钟前
5
0
第三方支付-返回与回调注意事项

不管是支付宝,微信,还是其它第三方支付,第四方支付,支付机构服务商只要涉及到钱的交易都要进行如下校验,全部成功了才视为成功订单 1.http请求是否成功 2.校验商户号 3.校验订单号及状态...

Shingfi
25分钟前
3
0
简述Java内存分配和回收策略以及Minor GC 和 Major GC(Full GC)

内存分配: 1. 栈区:栈可分为Java虚拟机和本地方法栈 2. 堆区:堆被所有线程共享,在虚拟机启动时创建,是唯一的目的是存放对象实例,是gc的主要区域。通常可分为两个区块年轻代和年老代。更...

DustinChan
31分钟前
4
0
Excel插入批注:可在批注插入文字、形状、图片

1.批注一直显示:审阅选项卡-------->勾选显示批注选项: 2.插入批注快捷键:Shift+F2 组合键 3.在批注中插入图片:鼠标右键点击批注框的小圆点【重点不可以在批注文本框内点击】----->调出批...

东方墨天
55分钟前
6
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部