通過sftp訪問伺服器記錄檔,逐條解析日誌後調用http介面實現業務功能

來源:互聯網
上載者:User

學習點sftp的訪問

學習點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 {
// 串連sftp伺服器
JSch jsch = new JSch();
sshSession = jsch.getSession("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");// 跳至檔案存放路徑

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

/**
* 調用伺服器介面
*/
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();
   // 取得輸入資料流,並使用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();
       // 中斷連線
       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();
}   

}

/**
* 擷取上一天的日期
*/
private String getBeforeDate()
{
//日誌日期,取前一天
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;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.