java sftp文件上传核心代码
来源:互联网 发布:软件系统测试报告用途 编辑:程序博客网 时间:2024/05/17 01:28
package sftp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Properties;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class SFTPUtil {
private String host = "172.30.104.40";
private String username="weblogic";
private String password="weblogic";
private int port = 22;
private ChannelSftp sftp = null;
private static final String linuxFileSeparator = "/";
public SFTPUtil(){}
public SFTPUtil(String host, String username, String password, int port){
this.host = host;
this.username = username;
this.password = password;
this.port = port;
}
/**
* connect server via sftp
*/
public void connect() {
try {
if(sftp != null){
System.out.println("sftp is not null");
}
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
System.out.println("Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
sshSession.setTimeout(1000*60*10);
System.out.println("Session connected.");
System.out.println("Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("Connected to " + host + ".");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Disconnect with server
*/
public void disconnect() {
if(this.sftp != null){
if(this.sftp.isConnected()){
this.sftp.disconnect();
}else if(this.sftp.isClosed()){
System.out.println("sftp is closed already");
}
}
}
public void download(String remoteDirectory, String remoteDownloadFileName, String localSaveFilePath) {
try {
this.sftp.cd(remoteDirectory);
File file = new File(localSaveFilePath);
this.sftp.get(remoteDownloadFileName, new FileOutputStream(file));
System.out.println("download remote file[" + remoteDirectory + linuxFileSeparator + remoteDownloadFileName + "] to local " + localSaveFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* upload one file to the server
*/
public void upload(String localFilePath, String remoteFileDirectory) {
try {
File file = new File(localFilePath);
if(file.isFile()){
System.out.println("localFile : " + file.getAbsolutePath());
System.out.println("remotePath:" + remoteFileDirectory);
this.sftp.cd(remoteFileDirectory);
this.sftp.put(new FileInputStream(file), file.getName());
System.out.println("=========upload file for " + localFilePath);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
/**
* @return the host
*/
public String getHost() {
return host;
}
/**
* @param host the host to set
*/
public void setHost(String host) {
this.host = host;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the port
*/
public int getPort() {
return port;
}
/**
* @param port the port to set
*/
public void setPort(int port) {
this.port = port;
}
/**
* @return the sftp
*/
public ChannelSftp getSftp() {
return sftp;
}
/**
* @param sftp the sftp to set
*/
public void setSftp(ChannelSftp sftp) {
this.sftp = sftp;
}
public static void main(String[] args) {
SFTPUtil ftp= new SFTPUtil();
ftp.connect();
ftp.upload("c:\\RpStatisticsAmountVO.java", "/home/weblogic/job");
ftp.download("/home/weblogic/job", "RpStatisticsAmountVO.java", "d:\\RpStatisticsAmountVO.java");
ftp.disconnect();
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Properties;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class SFTPUtil {
private String host = "172.30.104.40";
private String username="weblogic";
private String password="weblogic";
private int port = 22;
private ChannelSftp sftp = null;
private static final String linuxFileSeparator = "/";
public SFTPUtil(){}
public SFTPUtil(String host, String username, String password, int port){
this.host = host;
this.username = username;
this.password = password;
this.port = port;
}
/**
* connect server via sftp
*/
public void connect() {
try {
if(sftp != null){
System.out.println("sftp is not null");
}
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
System.out.println("Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
sshSession.setTimeout(1000*60*10);
System.out.println("Session connected.");
System.out.println("Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("Connected to " + host + ".");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Disconnect with server
*/
public void disconnect() {
if(this.sftp != null){
if(this.sftp.isConnected()){
this.sftp.disconnect();
}else if(this.sftp.isClosed()){
System.out.println("sftp is closed already");
}
}
}
public void download(String remoteDirectory, String remoteDownloadFileName, String localSaveFilePath) {
try {
this.sftp.cd(remoteDirectory);
File file = new File(localSaveFilePath);
this.sftp.get(remoteDownloadFileName, new FileOutputStream(file));
System.out.println("download remote file[" + remoteDirectory + linuxFileSeparator + remoteDownloadFileName + "] to local " + localSaveFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* upload one file to the server
*/
public void upload(String localFilePath, String remoteFileDirectory) {
try {
File file = new File(localFilePath);
if(file.isFile()){
System.out.println("localFile : " + file.getAbsolutePath());
System.out.println("remotePath:" + remoteFileDirectory);
this.sftp.cd(remoteFileDirectory);
this.sftp.put(new FileInputStream(file), file.getName());
System.out.println("=========upload file for " + localFilePath);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
/**
* @return the host
*/
public String getHost() {
return host;
}
/**
* @param host the host to set
*/
public void setHost(String host) {
this.host = host;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the port
*/
public int getPort() {
return port;
}
/**
* @param port the port to set
*/
public void setPort(int port) {
this.port = port;
}
/**
* @return the sftp
*/
public ChannelSftp getSftp() {
return sftp;
}
/**
* @param sftp the sftp to set
*/
public void setSftp(ChannelSftp sftp) {
this.sftp = sftp;
}
public static void main(String[] args) {
SFTPUtil ftp= new SFTPUtil();
ftp.connect();
ftp.upload("c:\\RpStatisticsAmountVO.java", "/home/weblogic/job");
ftp.download("/home/weblogic/job", "RpStatisticsAmountVO.java", "d:\\RpStatisticsAmountVO.java");
ftp.disconnect();
}
}
项目下载地址:http://download.csdn.net/download/qq_14946627/9282783
0 0
- java sftp文件上传核心代码
- JAVA-SFTP上传文件
- java上传文件通过SFTP
- sftp 文件上传 跨服务器 java
- Java连接SFTP服务器下载上传文件
- java中sftp文件上传与下载
- (1) java通过sftp上传文件
- Java实现FTP与SFTP文件上传
- ASP文件上传核心代码
- ajax文件上传核心代码
- JAVA代码实现SFTP文件下载
- Java代码实现SFTP文件下载
- sftp文件上传详解
- SFTP文件下载、上传
- SFTP上传文件详解
- sftp上传文件
- PHP-sftp文件上传
- sftp上传文件
- 项目导入后出现The import javax.servlet cannot be resolved报错怎么办
- android edittext限制输入小数位数,限制输入数字大小
- Android5.0(lollipop)新特性介绍(一)
- 集合框架-----LinkedList中的一些特有方法
- Spring邮件服务
- java sftp文件上传核心代码
- swift-使用NSURLConnection进行数据的提交和获取(POST与GET)
- android Activity与Fragment之间的通信
- 一个很详细的web.xml讲解
- Python在线爆破邮箱账号密码测试代码(亲测可用)
- 修改注册表添加IE信任站点及启用Activex控件
- Monkey测试简介
- input file
- Android中的显示单位