java轻松实现上传下载

package service;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import system.PropertiesUtil;

import dao.AH202_UserDao;

public class Log_Service
{
/**
* 日志管理模块
*
* @return
*/
  public File getLogFileObject() {
// 初始运行获取创建文件的年月
SimpleDateFormat da = new SimpleDateFormat("yyyyMM");
String file = "/Web200_" + da.format(new Date()) + ".log";
// 获取当前月
   String url = PropertiesUtil.readValue("logurl");
String currentMonth = da.format(new Date());
System.out.println(file.substring(8, 14));
// * 月份相同,不创建新的日志文件
if (currentMonth.equals(file.substring(8, 14))) {
File f = new File(url + file);
return f;
}
// * 月份不同,创建新的日志文件
if (!currentMonth.equals(file.substring(8, 14))) {
String file2 = "/Web200_" + da.format(new Date()) + ".log";
File f = new File(url + file2);
return f;
}
return null;
}
/**
* 日志写入模块
*
* @return
* @throws Exception
*/
public Boolean writeToLogFile(String log) throws Exception {
File file1 = getLogFileObject();
if (!file1.exists()) {
file1.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file1, true);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String words = "[" + df.format(new Date()).toString() + " Operator="
+ "]" + log;
words += "\r\n";
fos.write(words.getBytes());
fos.close();
return true;
}
public static void main(String[] args) throws Exception {
Log_Service log = new Log_Service();
log.writeToLogFile("测试写入Log");

}

}

猜你喜欢

转载自cm1993.iteye.com/blog/2227650
今日推荐