package com.boco.util;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jasypt.util.text.BasicTextEncryptor;
public class Utility {
public static Date analyseStringToDate(String value,String separator) throws Exception {
Date date = null;
if(value!=null&&separator!=null){
if(value.indexOf(separator)>-1){
if(separator.equals(".")){
separator = "\\"+separator;
}
String array[] = value.split(separator);
String year = array[0].trim();
String month = array[1].trim();
String day = null;
if(array.length>2){
day = array[2].trim();
}
if( day == null ){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
date = sdf.parse(year+"-"+month);
}else{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
date = sdf.parse(year+"-"+month+"-"+day);
}
}
}
//System.out.println(date.toString());
return date;
}
public static String null2String(String s) {
return s == null||s.equals("null") ? "" : s;
}
/**
* try to delete given file , try 10 times
* @param f
* @return true if file deleted success, nor false;
*/
public static boolean forceDelete(File f)
{
boolean result = false;
int tryCount = 0;
while(!result && tryCount++ <10)
{
//System.out.println("try to delete file "+ f.getName() +" cnt:"+tryCount);
System.gc();
result = f.delete();
}
return result;
}
public static String encode(String in)
{
try
{
return new String(in.getBytes("GBK"), "ISO8859_1");
}
catch (Exception e)
{
return in;
}
}
/**
* 获取当前请求IP
* @param request
* @return
*/
public static String getIpAddr(HttpServletRequest request) {
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.getRemoteAddr();
}
return ip;
}
/**
* 获取当前路径
*
* @param relPath 相对路径
*/
public static String getRealPath(String relPath) {
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String pathurl = path.substring(1, path.indexOf("WEB-INF"));
try {
pathurl = URLDecoder.decode(pathurl, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if(relPath != null && relPath.length() > 0) {
String temp = new String(relPath);
//处理前缀
if(temp.startsWith("/")) {
temp = temp.substring(1);
}
//处理后缀
if(!temp.endsWith("/")) {
temp = temp + "/";
}
pathurl = pathurl + temp;
}
return pathurl;
}
/**
* 获取当前路径
*/
public static String getRealPath() {
return getRealPath("");
}
/**
* 返回全路径文件名
*/
public static String getRealFile(String fileName) {
int pos = fileName.lastIndexOf('/');
if(pos < 0) {
pos = fileName.lastIndexOf('\\');
}
String path = fileName.substring(0, pos+1);
String name = fileName.substring(pos+1);
String fullname = getRealPath(path).concat(name);
return fullname;
}
/**
* 将当前日期格式化为字符串
* @param pattern 格式字符串
*/
public static String parseTodayToString(String pattern) {
return parseDateToString(pattern, new Date());
}
/**
* 将日期装换为字符串
* @param pattern 格式字符串
* @param date 日期
*/
public static String parseDateToString(String pattern, Date date) {
return new SimpleDateFormat(pattern).format(date);
}
/**
* 将字符串装换为日期
* @param pattern 格式字符串
* @param date 日期
*/
public static Date parseStringToDate(String pattern, String date) throws ParseException {
return new SimpleDateFormat(pattern).parse(date);
}
/**
* 分割字符串(大小写敏感)
* @param str 待分割字符串
* @param sign 分割符
* @return 分割后的字符串数组
*/
public static String[] splitString(String str, String sign)
{
int count = getStrCount(str, sign);
int j = 0;
String[] arr = new String[count];
for (int i = 0; i < count; i++)
{
arr[i] = "";
if (str.indexOf(sign) != -1)
{
j = str.indexOf(sign);
arr[i] = str.substring(0, j);
str = str.substring(j + 1);
}
else
{
arr[i] = str;
}
}
return arr;
}
/**
* 得到字符串中包含元素个数
* 如:"word1 word2 word3",返回3
*
* @param str 原字符串
* @param sign 分隔符
* @return 包含元素个数
*/
public static int getStrCount(String str, String sign)
{
if (str == null)
return 0;
StringTokenizer s = new StringTokenizer(str, sign);
return s.countTokens();
}
/**
* 将JSON字符串,通过Response对象输出至客户端
*/
public static void writeJsonToResponse(HttpServletResponse response, String content){
response.setContentType("application/x-json; charset=UTF-8");
PrintWriter out = null;
try {
out = response.getWriter();
out.println(content);
}
catch (IOException ex1) {
ex1.printStackTrace();
}
finally {
out.close();
}
}
/**
* 将HTML字符串,通过Response对象输出至客户端
*/
public static void writeHtmlToResponse(HttpServletResponse response, String content){
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = null;
try {
out = response.getWriter();
out.println(content);
}
catch (IOException ex1) {
ex1.printStackTrace();
}
finally {
out.close();
}
}
/**
* telnet密码加密
* @param password
* @return
*/
public static String encrypt(String password) {
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword("123");
return textEncryptor.encrypt(password);
}
/**
* telnet密码解密
* @param password
* @return
*/
public static String decrypt(String password) {
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword("123");
return textEncryptor.decrypt(password);
}
/**
* 得到MD5加密字符串
*/
public static String getMD5String(String message) {
byte[] bytes = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
bytes = md.digest(message.getBytes("utf-8"));
} catch (Exception e) {
e.printStackTrace();
}
int n;
StringBuffer buf = new StringBuffer("");
for (int i = 0; i < bytes.length; i++) {
n = bytes[i];
if(n < 0) {
n += 256;
}
if(n < 16) {
buf.append("0");
}
buf.append(Integer.toHexString(n));
}
String str = buf.toString();
return str;
}
public static void main(String[] args) {
getRealFile("aa/bb/cc/asdf.txt");
try {
analyseStringToDate("2007.6.6",".");
} catch (Exception e) {
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
log4j添加日志到数据库和文件中
共64个文件
jar:32个
class:7个
xml:7个
5星 · 超过95%的资源 需积分: 22 28 下载量 177 浏览量
2014-06-19
17:43:34
上传
评论
收藏 11.86MB ZIP 举报
温馨提示
使用log4j添加日志记录到数据库和文件中,log4j提供了非常强大的日志记录功能,为了弄这代码我整整研究了两天,访问时直接在地址栏输入日志添加的action就可以,没有写页面。
资源推荐
资源详情
资源评论
收起资源包目录
log.zip (64个子文件)
log
WebContent
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 1KB
example-servlet.xml 1KB
lib
dom4j-1.6.1.jar 307KB
c3p0-0.8.5.2.jar 391KB
aopalliance-1.0.jar 4KB
antlr-2.7.7.jar 435KB
spring-tx-4.0.0.M1.jar 242KB
mybatis-3.2.3.jar 697KB
quartz.jar 381KB
spring-web-4.0.0.M1.jar 608KB
commons-io-2.0.1.jar 156KB
ojdbc14.jar 1.47MB
spring-expression-4.0.0.M1.jar 198KB
mybatis-spring-1.2.0.jar 48KB
spring-context-4.0.0.M1.jar 895KB
spring-webmvc-4.0.0.M1.jar 624KB
neethi-3.0.2.jar 70KB
commons-beanutils-1.8.0.jar 226KB
spring-jdbc-4.0.0.M1.jar 407KB
fastjson-1.1.26.jar 348KB
spring-core-4.0.0.M1.jar 844KB
spring-context-support-4.0.0.M1.jar 126KB
aspectjweaver.jar 1.47MB
commons-logging-1.1.3.jar 61KB
spring-aop-4.0.0.M1.jar 340KB
javassist-3.17.1-GA.jar 695KB
jasypt-1.5.jar 125KB
log4j-api-2.0-beta9.jar 106KB
commons-fileupload-1.3.jar 67KB
spring-beans-4.0.0.M1.jar 617KB
log4j-1.2.17.jar 478KB
mysql.jar 464KB
spring-orm-4.0.0.M1.jar 304KB
stax-api-1.0.1.jar 26KB
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.wst.common.component 521B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 503B
org.eclipse.jdt.core.prefs 395B
src
mybatis-config.xml 274B
com
boco
controller
TestController.java 690B
util
Utility.java 8KB
yang
logging
Logger.java 2KB
LoggerPicker.java 800B
FileLogger.java 3KB
DatabaseLogger.java 2KB
applicationContext.xml 4KB
.project 1KB
.classpath 3KB
java_config
jdbc.properties 606B
log4j.properties 3KB
build
classes
mybatis-config.xml 274B
jdbc.properties 606B
log4j.properties 3KB
com
boco
controller
TestController.class 1KB
util
Utility.class 8KB
yang
logging
LoggerPicker$1.class 1KB
FileLogger.class 3KB
Logger.class 1KB
DatabaseLogger.class 2KB
LoggerPicker.class 543B
applicationContext.xml 4KB
共 64 条
- 1
资源评论
- 无与伦比刘元2014-09-17对我很有用,谢谢!!!
恋上白开水
- 粉丝: 4
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功