Access server log files through SFTP, parse logs one by one, and call the HTTP interface to implement business functions

Source: Internet
Author: User
Tags sftp access

Learning point: SFTP access

How to Use httpurlconnection

Import java. Io. bufferedreader;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java.net. httpurlconnection;
Import java.net. url;
Import java. Text. simpledateformat;
Import java. util. calendar;
Import java. util. date;
Import java. util. properties;

Import com. jcraft. jsch. channel;
Import com. jcraft. jsch. channelsftp;
Import com. jcraft. jsch. jsch;
Import com. jcraft. jsch. Session;

Public void parse (){

Session sshsession = NULL;
Channelsftp SFTP = NULL;
String beforedat = getbeforedate ();
Try {
// Connect to the SFTP Server
Jsch = new jsch ();
Sshsession = maid ("ftp-Music", "192.1.1.1", 50022 );
Sshsession. setpassword ("wtlv53xhcwgrqn66dq1c ");
Properties sshconfig = new properties ();
Sshconfig. Put ("stricthostkeychecking", "no ");
Sshsession. setconfig (sshconfig );
Sshsession. Connect ();
Channel channel = sshsession. openchannel ("SFTP ");
Channel. Connect ();
SFTP = (channelsftp) channel;
// SFTP. Cd ("mmlog"); // jump to the file storage path

Inputstream is = SFTP. Get ("/mmlog/download _" + getbeforedate () + ". txt ");
Bufferedreader BR = new bufferedreader (New inputstreamreader (is ));
Char [] CH = new char [1024];

While (Br. Read (CH )! =-1 ){
System. Out. println (CH );
// Httpurlconnection con = new
}

SFTP. Disconnect ();
Sshsession. Disconnect ();
} Catch (exception e ){
E. printstacktrace ();
} Finally {
If (SFTP. isconnected ()){
Try {
SFTP. Disconnect ();

} Catch (exception e ){
E. printstacktrace ();
}
}
}
}

/**
* Call the server interface
*/
Public void connectionclient (){
Try {
URL url = new URL ("http: // 127.0.0.1: 8080/mmserver/plug-in/SUB/orderproduct. aspx? Contentid = 1001010 ");
Httpurlconnection connection = (httpurlconnection) URL. openconnection ();
Connection. addrequestproperty ("GT", "ISME ");
Connection. setconnecttimeout (3000 );
Connection. setreadtimeout (3000 );

Connection. Connect ();
// Obtain the input stream and read it using Reader
Bufferedreader reader = new bufferedreader (New inputstreamreader (
Connection. getinputstream ()));
System. Out. println ("======================== ");
System. Out. println ("Contents of GET request ");
System. Out. println ("======================== ");
String lines;
While (lines = reader. Readline ())! = NULL ){
System. Out. println (lines );
}
Reader. Close ();
// Disconnect
Connection. Disconnect ();
System. Out. println ("======================== ");
System. Out. println ("Contents of GET request ends ");
System. Out. println ("======================== ");
} Catch (exception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

}

/**
* Get the date of the previous day
*/
Private string getbeforedate ()
{
// Log date, the previous day
Date current = new date ();
Calendar c = calendar. getinstance ();
C. settime (current );
Int date = C. Get (calendar. date );
C. Set (calendar. date, date-1 );
String befordatestr = new simpledateformat ("yyyymmdd"). Format (C. gettime ());
Return befordatestr;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.