您的位置:首页 > 其它

文件编码,图片格式,ip,日志访问等工具类

2014-10-30 15:27 573 查看
/**
* Copyright (c) 2005-2012 https://github.com/zhangkaitao *
* Licensed under the Apache License, Version 2.0 (the "License");
*/
package com.sishuok.es.common.utils;

import org.apache.commons.io.IOUtils;

import java.io.*;

/**
* 来自网络
*
* 判断文件编码 不保证100%正确性 但测试结果是一般的文件没问题
* <p/>
* 只能判断常见的GBK,UTF-16LE,UTF-16BE,UTF-8,其分别对应window下的记事本可另存为的编码类型ANSI,Unicode,Unicode big endian,UTF-8
* <p/>
* <p/>
* <p>User: Zhang Kaitao
* <p>Date: 13-7-5 下午12:09
* <p>Version: 1.0
*/
public class FileCharset {

private static final String DEFAULT_CHARSET = "GBK";

public static String getCharset(String fileName) {
return getCharset(new File(fileName));
}

/**
* 只能判断常见的GBK,UTF-16LE,UTF-16BE,UTF-8,其分别对应window下的记事本可另存为的编码类型ANSI,Unicode,Unicode big endian,UTF-8
*
* @param file
* @return
*/
public static String getCharset(File file) {
InputStream is = null;
try {
is = new FileInputStream(file);
return getCharset(new BufferedInputStream(is));
} catch (FileNotFoundException e) {
return DEFAULT_CHARSET;
} finally {
IOUtils.closeQuietly(is);
}
}
public static String getCharset(final BufferedInputStream is) {
String charset = DEFAULT_CHARSET;
byte[] first3Bytes = new byte[3];
try {
boolean checked = false;
is.mark(0);
int read = is.read(first3Bytes, 0, 3);
if (read == -1)
return charset;
if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
charset = "UTF-16LE";
checked = true;
} else if (first3Bytes[0] == (byte) 0xFE
&& first3Bytes[1] == (byte) 0xFF) {
charset = "UTF-16BE";
checked = true;
} else if (first3Bytes[0] == (byte) 0xEF
&& first3Bytes[1] == (byte) 0xBB
&& first3Bytes[2] == (byte) 0xBF) {
charset = "UTF-8";
checked = true;
}
is.reset();
if (!checked) {
int loc = 0;

while ((read = is.read()) != -1 && loc < 100) {
loc++;
if (read >= 0xF0)
break;
if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK
break;
if (0xC0 <= read && read <= 0xDF) {
read = is.read();
if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF)
// (0x80
// - 0xBF),也可能在GB编码内
continue;
else
break;
} else if (0xE0 <= read && read <= 0xEF) {// 也有可能出错,但是几率较小
read = is.read();
if (0x80 <= read && read <= 0xBF) {
read = is.read();
if (0x80 <= read && read <= 0xBF) {
charset = "UTF-8";
break;
} else
break;
} else
break;
}
}
}
is.reset();
} catch (Exception e) {
e.printStackTrace();
}

return charset;
}

}


/**
* Copyright (c) 2005-2012 https://github.com/zhangkaitao *
* Licensed under the Apache License, Version 2.0 (the "License");
*/
package com.sishuok.es.common.utils;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.ArrayUtils;

/**
* <p>User: Zhang Kaitao
* <p>Date: 13-2-12 下午9:29
* <p>Version: 1.0
*/
public class ImagesUtils {

private static final String[] IMAGES_SUFFIXES = {
"bmp", "jpg", "jpeg", "gif", "png", "tiff"
};

/**
* 是否是图片附件
*
* @param filename
* @return
*/
public static boolean isImage(String filename) {
if (filename == null || filename.trim().length() == 0) return false;
return ArrayUtils.contains(IMAGES_SUFFIXES, FilenameUtils.getExtension(filename).toLowerCase());
}

}


package com.sishuok.es.common.utils;

import javax.servlet.http.HttpServletRequest;

public class IpUtils {
private IpUtils() {
}

public static String getIpAddr(HttpServletRequest request) {
if (request == null) {
return "unknown";
}
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("X-Forwarded-For");
}
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("X-Real-IP");
}

if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}


package com.sishuok.es.common.utils;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

public class LogUtils {

public static final Logger ERROR_LOG = LoggerFactory.getLogger("es-error");
public static final Logger ACCESS_LOG = LoggerFactory.getLogger("es-access");

/**
* 记录访问日志
* [username][jsessionid][ip][accept][UserAgent][url][params][Referer]
*
* @param request
*/
public static void logAccess(HttpServletRequest request) {
String username = getUsername();
String jsessionId = request.getRequestedSessionId();
String ip = IpUtils.getIpAddr(request);
String accept = request.getHeader("accept");
String userAgent = request.getHeader("User-Agent");
String url = request.getRequestURI();
String params = getParams(request);
String headers = getHeaders(request);

StringBuilder s = new StringBuilder();
s.append(getBlock(username));
s.append(getBlock(jsessionId));
s.append(getBlock(ip));
s.append(getBlock(accept));
s.append(getBlock(userAgent));
s.append(getBlock(url));
s.append(getBlock(params));
s.append(getBlock(headers));
s.append(getBlock(request.getHeader("Referer")));
getAccessLog().info(s.toString());
}

/**
* 记录异常错误
* 格式 [exception]
*
* @param message
* @param e
*/
public static void logError(String message, Throwable e) {
String username = getUsername();
StringBuilder s = new StringBuilder();
s.append(getBlock("exception"));
s.append(getBlock(username));
s.append(getBlock(message));
ERROR_LOG.error(s.toString(), e);
}

/**
* 记录页面错误
* 错误日志记录 [page/eception][username][statusCode][errorMessage][servletName][uri][exceptionName][ip][exception]
*
* @param request
*/
public static void logPageError(HttpServletRequest request) {
String username = getUsername();

Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
String message = (String) request.getAttribute("javax.servlet.error.message");
String uri = (String) request.getAttribute("javax.servlet.error.request_uri");
Throwable t = (Throwable) request.getAttribute("javax.servlet.error.exception");

if (statusCode == null) {
statusCode = 0;
}

StringBuilder s = new StringBuilder();
s.append(getBlock(t == null ? "page" : "exception"));
s.append(getBlock(username));
s.append(getBlock(statusCode));
s.append(getBlock(message));
s.append(getBlock(IpUtils.getIpAddr(request)));

s.append(getBlock(uri));
s.append(getBlock(request.getHeader("Referer")));
StringWriter sw = new StringWriter();

while (t != null) {
t.printStackTrace(new PrintWriter(sw));
t = t.getCause();
}
s.append(getBlock(sw.toString()));
getErrorLog().error(s.toString());

}

public static String getBlock(Object msg) {
if (msg == null) {
msg = "";
}
return "[" + msg.toString() + "]";
}

protected static String getParams(HttpServletRequest request) {
Map<String, String[]> params = request.getParameterMap();
return JSON.toJSONString(params);
}

private static String getHeaders(HttpServletRequest request) {
Map<String, List<String>> headers = Maps.newHashMap();
Enumeration<String> namesEnumeration = request.getHeaderNames();
while(namesEnumeration.hasMoreElements()) {
String name = namesEnumeration.nextElement();
Enumeration<String> valueEnumeration = request.getHeaders(name);
List<String> values = Lists.newArrayList();
while(valueEnumeration.hasMoreElements()) {
values.add(valueEnumeration.nextElement());
}
headers.put(name, values);
}
return JSON.toJSONString(headers);
}

protected static String getUsername() {
return (String) SecurityUtils.getSubject().getPrincipal();
}

public static Logger getAccessLog() {
return ACCESS_LOG;
}

public static Logger getErrorLog() {
return ERROR_LOG;
}

}


/**
* Copyright (c) 2005-2012 https://github.com/zhangkaitao *
* Licensed under the Apache License, Version 2.0 (the "License");
*/
package com.sishuok.es.common.utils;

import org.springframework.context.MessageSource;

/**
* <p>User: Zhang Kaitao
* <p>Date: 13-2-9 下午8:49
* <p>Version: 1.0
*/
public class MessageUtils {

private static MessageSource messageSource;

/**
* 根据消息键和参数 获取消息
* 委托给spring messageSource
*
* @param code 消息键
* @param args 参数
* @return
*/
public static String message(String code, Object... args) {
if (messageSource == null) {
messageSource = SpringUtils.getBean(MessageSource.class);
}
return messageSource.getMessage(code, args, null);
}

}


/**
* Copyright (c) 2005-2012 https://github.com/zhangkaitao *
* Licensed under the Apache License, Version 2.0 (the "License");
*/
package com.sishuok.es.common.utils;

/**
* <p>User: Zhang Kaitao
* <p>Date: 13-6-2 上午6:48
* <p>Version: 1.0
*/
public class PrettyMemoryUtils {

private static final int UNIT = 1024;

/**
* @param byteSize 字节
* @return
*/
public static String prettyByteSize(long byteSize) {

double size = 1.0 * byteSize;

String type = "B";
if((int)Math.floor(size / UNIT) <= 0) { //不足1KB
type = "B";
return format(size, type);
}

size = size / UNIT;
if((int)Math.floor(size / UNIT) <= 0) { //不足1MB
type = "KB";
return format(size, type);
}

size = size / UNIT;
if((int)Math.floor(size / UNIT) <= 0) { //不足1GB
type = "MB";
return format(size, type);
}

size = size / UNIT;
if((int)Math.floor(size / UNIT) <= 0) { //不足1TB
type = "GB";
return format(size, type);
}

size = size / UNIT;
if((int)Math.floor(size / UNIT) <= 0) { //不足1PB
type = "TB";
return format(size, type);
}

size = size / UNIT;
if((int)Math.floor(size / UNIT) <= 0) {
type = "PB";
return format(size, type);
}
return ">PB";
}

private static String format(double size, String type) {
int precision = 0;

if(size * 1000 % 10 > 0) {
precision = 3;
} else if(size * 100 % 10 > 0) {
precision = 2;
} else if(size * 10 % 10 > 0) {
precision = 1;
} else {
precision = 0;
}

String formatStr = "%." + precision + "f";

if("KB".equals(type)) {
return String.format(formatStr, (size)) + "KB";
} else if("MB".equals(type)) {
return String.format(formatStr, (size)) + "MB";
} else if("GB".equals(type)) {
return String.format(formatStr, (size)) + "GB";
} else if("TB".equals(type)) {
return String.format(formatStr, (size)) + "TB";
} else if("PB".equals(type)) {
return String.format(formatStr, (size)) + "PB";
}
return  String.format(formatStr, (size)) + "B";
}

public static void main(String[] args) {
System.out.println(PrettyMemoryUtils.prettyByteSize(1023));
System.out.println(PrettyMemoryUtils.prettyByteSize(1L * UNIT));
System.out.println(PrettyMemoryUtils.prettyByteSize(1L * UNIT * UNIT));
System.out.println(PrettyMemoryUtils.prettyByteSize(1L * UNIT * 1023));
System.out.println(PrettyMemoryUtils.prettyByteSize(1L * 1023 * 1023 * 1023));
System.out.println(PrettyMemoryUtils.prettyByteSize(1L * UNIT * UNIT * UNIT));
System.out.println(PrettyMemoryUtils.prettyByteSize(1L * UNIT * UNIT * UNIT * UNIT));
System.out.println(PrettyMemoryUtils.prettyByteSize(1L * UNIT * UNIT * UNIT * UNIT * UNIT));
System.out.println(PrettyMemoryUtils.prettyByteSize(1L * UNIT * UNIT * UNIT * UNIT * UNIT * UNIT));
}

}

/**
* Copyright (c) 2005-2012 https://github.com/zhangkaitao *
* Licensed under the Apache License, Version 2.0 (the "License");
*/
package com.sishuok.es.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.ocpsoft.prettytime.PrettyTime;

import java.util.Date;

/**
* <p>User: Zhang Kaitao
* <p>Date: 13-3-22 下午12:18
* <p>Version: 1.0
*/
public class PrettyTimeUtils {

/**
* 显示秒值为**年**月**天 **时**分**秒  如1年2个月3天 10小时
*
* @return
*/
public static final String prettySeconds(int totalSeconds) {
StringBuilder s = new StringBuilder();
int second = totalSeconds % 60;
if (totalSeconds > 0) {
s.append("秒");
s.append(StringUtils.reverse(String.valueOf(second)));
}

totalSeconds = totalSeconds / 60;
int minute = totalSeconds % 60;
if (totalSeconds > 0) {
s.append("分");
s.append(StringUtils.reverse(String.valueOf(minute)));
}

totalSeconds = totalSeconds / 60;
int hour = totalSeconds % 24;
if (totalSeconds > 0) {
s.append(StringUtils.reverse("小时"));
s.append(StringUtils.reverse(String.valueOf(hour)));
}

totalSeconds = totalSeconds / 24;
int day = totalSeconds % 31;
if (totalSeconds > 0) {
s.append("天");
s.append(StringUtils.reverse(String.valueOf(day)));
}

totalSeconds = totalSeconds / 31;
int month = totalSeconds % 12;
if (totalSeconds > 0) {
s.append("月");
s.append(StringUtils.reverse(String.valueOf(month)));
}

totalSeconds = totalSeconds / 12;
int year = totalSeconds;
if (totalSeconds > 0) {
s.append("年");
s.append(StringUtils.reverse(String.valueOf(year)));
}
return s.reverse().toString();
}

/**
* 美化时间 如显示为 1小时前 2分钟前
*
* @return
*/
public static final String prettyTime(Date date) {
PrettyTime p = new PrettyTime();
return p.format(date);

}

public static final String prettyTime(long millisecond) {
PrettyTime p = new PrettyTime();
return p.format(new Date(millisecond));
}

public static void main(String[] args) {
System.out.println(PrettyTimeUtils.prettyTime(new Date()));
System.out.println(PrettyTimeUtils.prettyTime(123));

System.out.println(PrettyTimeUtils.prettySeconds(10));
System.out.println(PrettyTimeUtils.prettySeconds(61));
System.out.println(PrettyTimeUtils.prettySeconds(3661));
System.out.println(PrettyTimeUtils.prettySeconds(36611));
System.out.println(PrettyTimeUtils.prettySeconds(366111));
System.out.println(PrettyTimeUtils.prettySeconds(3661111));
System.out.println(PrettyTimeUtils.prettySeconds(36611111));
}
}


/**
* Copyright (c) 2005-2012 https://github.com/zhangkaitao *
* Licensed under the Apache License, Version 2.0 (the "License");
*/
package com.sishuok.es.common.utils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
* <p>User: Zhang Kaitao
* <p>Date: 13-2-23 下午1:25
* <p>Version: 1.0
*/
public class ReflectUtils {

/**
* 得到指定类型的指定位置的泛型实参
*
* @param clazz
* @param index
* @param <T>
* @return
*/
public static <T> Class<T> findParameterizedType(Class<?> clazz, int index) {
Type parameterizedType = clazz.getGenericSuperclass();
//CGLUB subclass target object(泛型在父类上)
if (!(parameterizedType instanceof ParameterizedType)) {
parameterizedType = clazz.getSuperclass().getGenericSuperclass();
}
if (!(parameterizedType instanceof  ParameterizedType)) {
return null;
}
Type[] actualTypeArguments = ((ParameterizedType) parameterizedType).getActualTypeArguments();
if (actualTypeArguments == null || actualTypeArguments.length == 0) {
return null;
}
return (Class<T>) actualTypeArguments[0];
}
}


/**
* Copyright (c) 2005-2012 https://github.com/zhangkaitao *
* Licensed under the Apache License, Version 2.0 (the "License");
*/
package com.sishuok.es.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public final class SpringUtils implements BeanFactoryPostProcessor {

private static ConfigurableListableBeanFactory beanFactory; // Spring应用上下文环境

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
SpringUtils.beanFactory = beanFactory;
}

/**
* 获取对象
*
* @param name
* @return Object 一个以所给名字注册的bean的实例
* @throws org.springframework.beans.BeansException
*
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) throws BeansException {
return (T) beanFactory.getBean(name);
}

/**
* 获取类型为requiredType的对象
*
* @param clz
* @return
* @throws org.springframework.beans.BeansException
*
*/
public static <T> T getBean(Class<T> clz) throws BeansException {
@SuppressWarnings("unchecked")
T result = (T) beanFactory.getBean(clz);
return result;
}

/**
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
*
* @param name
* @return boolean
*/
public static boolean containsBean(String name) {
return beanFactory.containsBean(name);
}

/**
* 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
*
* @param name
* @return boolean
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
*
*/
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return beanFactory.isSingleton(name);
}

/**
* @param name
* @return Class 注册对象的类型
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
*
*/
public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
return beanFactory.getType(name);
}

/**
* 如果给定的bean名字在bean定义中有别名,则返回这些别名
*
* @param name
* @return
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
*
*/
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return beanFactory.getAliases(name);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: