JSCH實現檔案上傳的代碼執行個體

來源:互聯網
上載者:User

標籤:pat   cal   org   ann   方式   cas   src   mail   ssi   

package com.vcredit.ddcash.monitor.sendmail;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

/**
* created by ding 2016-12-02 17:28 Friday
* */
public class FtpsFileList {
private static final Logger LOG = LoggerFactory.getLogger(FtpsFileList.class);

public static void main(String[] args) {
listFileNames("****","**", "root", "}KCxBZC6IO]hWC>CfDkgD<41WhTP(", "/usr");

}

private static List<String> listFileNames(String host, int port, String username, final String password,
String dir) {
List<String> list = new ArrayList<String>();
ChannelSftp sftp = null;
Channel channel = null;
Session sshSession = null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
sshSession = jsch.getSession(username, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
LOG.debug("Session connected!");
channel = sshSession.openChannel("sftp");
channel.connect();
LOG.debug("Channel connected!");
sftp = (ChannelSftp) channel;
// 檔案上傳 下面這種方式也可以
// File file = new File("15888888_back_cut.jpg");
// file.getAbsolutePath();
// if (!file.exists()){
// file.createNewFile();
// }
// FileInputStream in = new FileInputStream(file);
// sftp.put(in, "/usr/local/ding/15888888_back_cut.jpg1");

// sftp.put(src, dst);//src本地檔案,dst 伺服器上的檔案
sftp.put("15888888_back_cut.jpg", "/usr/local/ding/");
// 檔案下載
sftp.cd("/usr/local/ding/");
// sftp.get(src, dst); src為伺服器上的檔案,dst問本地的目錄,如果只是路徑 ,預設檔案名稱一致
sftp.get("/usr/local/ding/15888888_back_cut.jpg", "D:/");

Vector<?> vector = sftp.ls(dir);
for (Object item : vector) {
LsEntry entry = (LsEntry) item;
System.out.println(entry.getFilename());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
closeChannel(sftp);
closeChannel(channel);
closeSession(sshSession);
}
return list;
}

private static void closeChannel(Channel channel) {
if (channel != null) {
if (channel.isConnected()) {
channel.disconnect();
}
}
}

private static void closeSession(Session session) {
if (session != null) {
if (session.isConnected()) {
session.disconnect();
}
}
}
}

JSCH實現檔案上傳的代碼執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.