Auto submit Daily Report

来源:互联网 发布:斜率优化 编辑:程序博客网 时间:2024/05/17 13:43
public class ADR {       private static int YEAR=0;    private static int MONTH=1;    private static int DAY=2;       /**     *     * @param user     * @param pass     * @return     * @throws Exception     */    private static String loginJh(String user , String pass) throws Exception{        URL url =  new URL("http://jh.livedoor.cn/cgi-bin/cookie/login.pl");        HttpURLConnection hconn=(HttpURLConnection)url.openConnection();        hconn.setDoOutput(true);        hconn.setDoInput(true);        hconn.setRequestMethod("POST");               /**         * fill login name and pass         */        hconn.getOutputStream().write(("name="+user+"&password="+pass).getBytes());               hconn.getOutputStream().flush();        hconn.getOutputStream().close();               /**         * check response code.         */        if(hconn.getResponseCode()!=HttpURLConnection.HTTP_OK){            throw new Exception("Login jh err! description:"+hconn.getResponseMessage());        }               /**         * Get cookie         */        StringBuffer cookie =new StringBuffer();        Map> header=hconn.getHeaderFields();        List c=header.get("Set-Cookie");        for(int i=0;c!=null && i');";        BufferedReader bis =new BufferedReader(new InputStreamReader(hconn.getInputStream() , "GBK"));        StringBuffer sb=new StringBuffer();        while(bis.ready()){            sb.append((char)bis.read());        }        if(!sb.toString().equals(target_html)){            throw new Exception("Login target web page is err!");        }        hconn.disconnect();               return cookie.toString();    }          /**     *     * @param cookie     * @throws Exception     */    private static void writeDailyReport(String cookie , String pj_id , String mem_id , String rumor) throws Exception{               /**         * project id          * Security = 80         */        String p1="pj_id="+pj_id+"&";               /**         * mem_id         * huangcm mem_id=430         */        String p1_5="mem_id="+mem_id+"&";               /**         * submit date&time         */        String sdate[]=getStrDate(new Date());        String p2="c_year="+sdate[YEAR]+"&c_month="+sdate[MONTH]+"&c_day="+sdate[DAY]+"&";        String p3="start_hour=8&start_min=0&end_hour=19&end_min=30&";               /**         * work time 8         */        String p4="job_time"+pj_id+"=8&";        String p5="job_content"+pj_id+"="+rumor+"&";        String p6="sum="+rumor+"&";        String p7="question=&";        String p8="experience=&";               /**         * next day         */        String ndate[]=getStrDate(ADR.getNextDay());        String p9="n_year="+ndate[YEAR]+"&n_month="+ndate[MONTH]+"&n_day="+ndate[DAY]+"&";               /**         * next plan         */        String p10="plan="+rumor;                      /**         * submit daily report         */         URL url2 =  new URL("http://jh.livedoor.cn//cgi-bin/project/pm_rp_add_done.pl");        HttpURLConnection hconn=(HttpURLConnection)url2.openConnection();        hconn.setDoOutput(true);        hconn.setDoInput(true);        hconn.setRequestMethod("POST");        hconn.setRequestProperty("Cookie", cookie);        hconn.getOutputStream().write((p1+p1_5+p2+p3+p4+p5+p6+p7+p8+p9+p10).getBytes());        hconn.getOutputStream().flush();        hconn.getOutputStream().close();               if(hconn.getResponseCode()!=HttpURLConnection.HTTP_OK){            throw new Exception("Write daily report err! description:["+hconn.getResponseCode()+"]"+hconn.getResponseMessage());        }       //        BufferedReader bis =new BufferedReader(new InputStreamReader(hconn.getInputStream() , "GBK"));//        StringBuffer sb=new StringBuffer();//        while(bis.ready()){//            sb.append(bis.readLine()+"/n");//        }//        System.out.println(sb.toString());               hconn.disconnect();    }          /**     *     * @param format     * @return     */    private static String[] getStrDate(Date date){        String r[]=new String[3];        r[YEAR]=new SimpleDateFormat("yyyy").format(date);        r[MONTH]=String.valueOf(Integer.parseInt(new SimpleDateFormat("MM").format(date)));        r[DAY]=String.valueOf(Integer.parseInt(new SimpleDateFormat("dd").format(date)));        return r;    }    private static Date getNextDay(){        Calendar c=Calendar.getInstance();        c.add(Calendar.DAY_OF_MONTH,1);        return c.getTime();    }          /**     * Send mail to mail.livedoor.cn     * @param user     * @param pass     * @param subject     * @param content     */    private static void sendMail(String user,String pass,String subject,String content){                Socket socketSmtpServer = null;                  BufferedWriter dos = null;         BufferedReader dis = null;         try {             socketSmtpServer = new Socket("mail.livedoor.cn", 25);             socketSmtpServer.setSoTimeout(500);             dos = new BufferedWriter(new OutputStreamWriter(socketSmtpServer.getOutputStream()));             dis = new BufferedReader(new InputStreamReader (socketSmtpServer.getInputStream()));                          try{                 dis.readLine();                 dis.readLine();             }catch(Exception dx){}                          dos.write("HELO "+user+"/r/n");dos.flush();             System.out.println("R_HELO:"+dis.readLine());                          // auth login             dos.write("AUTH LOGIN/r/n");dos.flush();             System.out.println( new String(new BASE64Decoder().decodeBuffer(dis.readLine())));                          String e_user=new BASE64Encoder().encode(user.getBytes());             dos.write(new String(e_user)+"/r/n");dos.flush();             System.out.println("R_USER:"+ new String(new BASE64Decoder().decodeBuffer(dis.readLine())));                          String e_password=new BASE64Encoder().encode(pass.getBytes());             dos.write(new String(e_password)+"/r/n");dos.flush();             System.out.println("R_PASS:"+ dis.readLine());             dos.write("MAIL FROM:"+user+"@livedoor.cn/r/n");dos.flush();             System.out.println("R_MAIL FROM:"+dis.readLine());                          dos.write("RCPT TO:huangcm@livedoor.cn/r/n");dos.flush();             System.out.println("RCPT TO:"+dis.readLine());                                       dos.write("DATA/r/n");dos.flush();             System.out.println("R_DATA:"+dis.readLine());                          dos.write("To: myself/r/n");             dos.write("From: AutoDailyReport/r/n");             dos.write("Subject: "+subject+"/r/n");             dos.write("/r/n"+content+"/r/n");             dos.write("./r/n");             dos.flush();                          System.out.println("R.:"+dis.readLine());                          dos.write("QUIT/r/n");dos.flush();             System.out.println("R_QUIT:"+dis.readLine());                      }catch(Exception dx){             dx.printStackTrace();         }              }          public static void main(String args[]) throws Exception{               if(args.length!=7){            System.out.println("java ADR jh_name jh_pass project_id mem_id rumor mail_name mail_pass");            return;        }               String name=args[0];        String pass=args[1];        String pid =args[2];        String mid =args[3];        String rumor=args[4];        String m_user=args[5];        String m_pass=args[6];               try{            String cookie=ADR.loginJh(name,pass);            ADR.writeDailyReport(cookie,pid,mid,rumor);            sendMail(m_user,m_pass,"AutoDailyReport OK","Submit Daily Report OK.");        }catch(Exception dx){            dx.printStackTrace();            sendMail(m_user,m_pass,"AutoDailyReport Error",dx.getMessage());        }           }   }//---------------------------------------------------//Add in Linux crontab (shell script)#/bin/bshLOG_FILE=/opt/AutoDailyReport/haha.logCOMMAND='/usr/java/jdk1.5.0_04/bin/java -cp /opt/AutoDailyReport com.livedoor.xxx.ADR jh_name jh_pass nn nnn coding mail_name mail_pass'echo `date`>>$LOG_FILEecho `${COMMAND}`>>$LOG_FILEecho '-------------------------------------------'>>$LOG_FILE//------------------------------------------------------//Windows .bat 批处理@echo offrem ***windows bat***set cp="D:/AutoDailyReport"set log="%cp%/haha.log"set jp="d:/java/jdk1.5.0_04/bin/java"set rcmd="com.livedoor.xxx.ADR"set parm1="huangchengming"set parm2="skywest"set parm3="80"set parm4="430"set parm5="coding"set parm6="huangcm"set parm7="skywest"date /t >> %log%%jp% -cp %cp% %rcmd% %parm1% %parm2% %parm3% %parm4% %parm5% %parm6% %parm7% >> %log%echo "====================================================================" >> %log%
 
原创粉丝点击