故事的起因是这样的,因上线工作量比较大,测试小y希望晚上的上线可以一部分提前中午上线。但是不料,中午上线不到一个小时的时间,用户注册数据进去了200+条,而上线期间程序出了问题,只有部分用户信息入库,也就是client表信息完整,可是其他附属表信息缺失。不过好在,程序的日志里对用户传入的信息打了日志,于是我们为了避免这200+条用户投诉,进行了如下操作:
1.从这是时间的日志里读取有用的日志信息
2.把这段有用的日志信息进行处理,取出用户相应的信息字段
3,通过仿页面表单提交的方式调用提交接口进行数据入库补齐处理
首先,我们来看一下有用的日志信息样式:
于是,我从tomcat里取出这段时间的日志文件,进行了读取,取出含关键语句的行,然后通过split对每一行提取有用数据信息,并调用访表单提交方法。
首先,逐行读取日志文件:(代码如下)
/**
* 读取一行一行的文件
*
* @param filename
* @return
* @throws IOException
*/
public static List<String> readLinedFile(String filename){
List<String> filecon = new ArrayList<String>();
String m = "";
BufferedReader file = null;
try{
file = new BufferedReader(new FileReader(filename));
while ((m = file.readLine()) != null) {
if (!m.equals("")) // 不需要读取空行
{
filecon.add(m);
}
}
file.close();
}catch(IOException e){
e.printStackTrace();
}
return filecon;
}
其次,提取有用信息,并逐条调用访表单提交方法:
/**
* 写入一行一行的文件
* @param lst 要写入的字符串数组
* @param filename 要写入的文件路径
* @return
* @throws IOException
*/
public static void writeLinedFileClient(List lst, String filePath) throws IOException {
File file = new File(filePath);
BufferedWriter out = new BufferedWriter(new FileWriter(file, false));
for (int i = 0; i < lst.size(); i++) {
String temp = (String) lst.get(i);
//client
if (temp != null && temp.trim().length() != 0 && (temp.contains("进入ClientAction中add方法传入的参数为"))) {
List<String> ls = new ArrayList<String>();
temp = temp.split("电话")[1];
String[] fenge ={"电话","表单来源宝宝生日","openId","姓名","是否接受协议","地址","指定代码","paperIn","typeidactivityidsacode关注问题","准备使用品牌","over"};
int ji = 1;
String[] data=new String[13];
while(temp.split(fenge[ji]).length>1 && ji<fenge.length-1){
String[] a = temp.split(fenge[ji]);
out.write(a[0]+"~");
data[ji]=a[0].trim();
temp = a[1];
ji++;
}
data[ji]=temp.trim();
out.write(temp +" ");
<span style="white-space:pre"> </span>//testPost方法为仿页面表单提交
String result = testPost(data[3],data[1],data[4],data[2],"WX",data[5],data[7],null,data[8],data[10],data[9]);
out.write(result +"");
out.newLine();
}
}
out.close();
out = null;
file=null;
}
testPost方法实现:
public static String testPost(String openId,String cellphone,String name,String babybirth,String sourceType,String acceptLastedInfo,String code,String paperIn,String sacode,String intention,String attention) throws IOException {
/**
* 首先要和URL下的URLConnection对话。 URLConnection可以很容易的从URL得到。比如: // Using
* java.net.URL and //java.net.URLConnection
*
* 使用页面发送请求的正常流程:在页面http://www.faircanton.com/message/loginlytebox.asp中输入用户名和密码,然后按登录,
* 跳转到页面http://www.faircanton.com/message/check.asp进行验证
* 验证的的结果返回到另一个页面
*
* 使用java程序发送请求的流程:使用URLConnection向http://www.faircanton.com/message/check.asp发送请求
* 并传递两个参数:用户名和密码
* 然后用程序获取验证结果
*/
URL url = new URL("http://*******************/bugXFclient_add.action"); //为安全起见对域名信息进行了屏蔽,望见谅
URLConnection connection = url.openConnection();
/**
* 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。
* 通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做:
*/
connection.setDoOutput(true);
/**
* 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ...
*/
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
out.write("openId="+openId+"&cellphone="+cellphone+"&name="+name+"&babybirth="+babybirth+"&sourceType="+sourceType+"&acceptLastedInfo="+acceptLastedInfo+"&code="+code+"&paperIn="+paperIn+"&sacode="+sacode+"&intention="+intention+"&attention="+attention); //向页面传递数据。post的关键所在!
// remember to clean up
out.flush();
out.close();
/**
* 这样就可以发送一个看起来象这样的POST:
* POST /jobsearch/jobsearch.cgi HTTP 1.0 ACCEPT:
* text/plain Content-type: application/x-www-form-urlencoded
* Content-length: 99 username=bob password=someword
*/
// 一旦发送成功,用以下方法就可以得到服务器的回应:
String sCurrentLine;
String sTotalString;
sCurrentLine = "";
sTotalString = "";
InputStream l_urlStream;
l_urlStream = connection.getInputStream();
// 传说中的三层包装阿!
BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));
while ((sCurrentLine = l_reader.readLine()) != null) {
sTotalString += sCurrentLine + "/r/n";
System.out.println(sTotalString);
}
System.out.println(sTotalString);
return sTotalString;
}
最后,我们只需要用main方法调用一下即可:
public static void main(String[] args) throws IOException{
<span style="white-space:pre"> </span>//<span style="font-family: Arial, Helvetica, sans-serif;">mjnwx2.txt为日志文件,</span><span style="font-family: Arial, Helvetica, sans-serif;">clientusefulready2.txt用于存储有用的用户信息和每条信息的提交返回结果</span><span style="font-family: Arial, Helvetica, sans-serif;">
</span> List<String> ls = readLinedFile("D://mjnwxlog/mjnwx2.txt");
writeLinedFileClient(ls,"D://mjnwxlog/clientusefulready2.txt");
}
给自己挖了一个坑,然后把自己放进去把坑填好,希望对您有所帮助。
END