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;
}