📄 userauth.java
字号:
package com.sxit.wap.threads;
import java.util.*;
import java.io.*;
import java.sql.*;
import sun.net.*;
import sun.net.ftp.*;
public class UserAuth extends Thread {
Connection conn = null;
//FTPClient client;
File file = null;
public UserAuth() {
System.out.println("user auth started");
//线程由TimeController来调度
}
public void run() {
try {
System.out.println("user auth thread started");
file = new File("user_auth" + new java.text.SimpleDateFormat("yyyyMMdd").format (new java.util.Date()) + ".txt");
System.out.println("begin to get file....");
getFile("220.195.192.85", "wap", "wap", "", file.getName());
System.out.println("get file success");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
Database.updateBySql("DELETE FROM WAP_USER_WHITE");
System.out.println("delete white of yestoday success");
int errorCount = 0;
StringBuffer errorUserList = new StringBuffer();
int insertTotal = 0;
while((line=reader.readLine())!=null){
String userMdn = getUserMdnByLine(line);
if (userMdn==null || "".equals(userMdn)) continue;
String sql = "INSERT INTO WAP_USER_WHITE VALUES('" + userMdn + "')";
insertTotal++;
if (insertTotal%100 == 0) sleep(1000);
if (conn == null || conn.isClosed()) conn = Database.getConnection();
try {
Database.updateBySql(sql, conn);
System.out.println("insert black user " + userMdn + " success");
}catch(Exception e) {
e.printStackTrace();
if (!isUserExists(userMdn)) {
errorCount++;
errorUserList.append(userMdn + "\t");
if (errorCount > 100) break;
}
}
}
int tableTotal = getRowCountOfWhite();
if (errorCount == 0 && insertTotal == tableTotal) {
if (conn == null || conn.isClosed()) {conn = Database.getConnection();}
try {
conn.setAutoCommit(false);
String sql = "INSERT INTO WAP_USER_SUB_HIS_BLACK (SELECT * FROM WAP_USER_SUB_HIS WHERE " + DateUtil.getDateCompareSql(Database.dbType, "SUB_DATE", DateUtil.getSomeDate(-1), DateUtil.getCurrTime()) + " AND USER_MDN NOT IN (SELECT USER_MDN FROM WAP_USER_WHITE))";
System.out.println(sql);
Database.updateBySql(sql, conn);
System.out.println("backup user sub history data of yestoday success");
sql = "DELETE FROM WAP_USER_SUB_HIS WHERE " + DateUtil.getDateCompareSql(Database.dbType, "SUB_DATE", DateUtil.getSomeDate(-1), DateUtil.getCurrTime()) + " AND USER_MDN NOT IN (SELECT USER_MDN FROM WAP_USER_WHITE)";
System.out.println(sql);
Database.updateBySql(sql, conn);
System.out.println("delete black user sub history data of yestory success");
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
conn.rollback();
conn.setAutoCommit(true);
}
} else {
System.out.println("there are " + errorCount + " error while insert white user, the white user list of fail to insert database :\n" + new String(errorUserList));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {conn.close();conn=null;} catch (Exception e){}
}
}
public int getRowCountOfWhite() throws Exception {
String sql = "SELECT COUNT(*) FROM WAP_USER_WHITE";
return Database.getRowCountBySql(sql);
}
public boolean isUserExists(String userMdn) throws Exception {
String sql = "SELECT COUNT(*) FROM WAP_USER_WHITE WHERE USER_MDN = ' " + userMdn + " '";
return Database.getRowCountBySql(sql) > 0;
}
public String getUserMdnByLine(String line) {
if (!line.startsWith("3")) {
System.out.println(line + "is passed");
return "";
}
if (line.length() != 11) {
System.out.println(line + "is passed");
return "";
}
if (!line.endsWith("00")) {
System.out.println(line + "is passed");
return "";
}
return "13" + line.substring(0, 9);
}
public static void getFile(String server, String user, String password, String path, String fileName) throws IOException {
FtpClient ftpClient=new FtpClient();
ftpClient.openServer(server);
ftpClient.login(user, password);
if (path.length()!=0) ftpClient.cd(path);
ftpClient.binary();
TelnetInputStream is=ftpClient.get(fileName);
File file_out=new File(fileName);
FileOutputStream os=new
FileOutputStream(file_out);
byte[] bytes=new byte[1024];
int c;
while ((c=is.read(bytes))!=-1) {
os.write(bytes,0,c);
}
is.close();
os.close();
ftpClient.closeServer();
}
public static void main(String[] args) {
UserAuth userAuth1 = new UserAuth();
userAuth1.start();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -