`
lvxb
  • 浏览: 9602 次
  • 性别: Icon_minigender_1
  • 来自: 西安
文章分类
社区版块
存档分类
最新评论

Sftp上传下载

 
阅读更多
package org.codedata;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
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 FtpImpl {

private String host = "192.168.1.15";
private String username = "liz";
private String password = "liz";
private int port = 2033;
private ChannelSftp sftp = null;
private String localPath = "/Users/liz/Documents";
private String remotePath = "/Users/liz/MyDocuments";
private String fileListPath = "/Users/liz/Documents/Java/workspace/MyTools/conf/file.txt";
private final String seperator = "/";

/**
* 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();
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() {
// TODO Auto-generated method stub

}

private void download(String directory, String downloadFile,
String saveFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* upload all the files to the server
*/
public void upload() {
List<String> fileList = this.getFileEntryList(fileListPath);
try {
if (fileList != null) {
for (String filepath : fileList) {
String localFile = this.localPath + this.seperator
+ filepath;
File file = new File(localFile);

if (file.isFile()) {
System.out.println("localFile : "
+ file.getAbsolutePath());
String remoteFile = this.remotePath + this.seperator
+ filepath;
System.out.println("remotePath:" + remoteFile);
File rfile = new File(remoteFile);
String rpath = rfile.getParent();
try {
createDir(rpath, sftp);
} catch (Exception e) {
System.out.println("*******create path failed"
+ rpath);
}

this.sftp
.put(new FileInputStream(file), file.getName());
System.out.println("=========upload down for "
+ localFile);
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/**
* create Directory
*
* @param filepath
* @param sftp
*/
private void createDir(String filepath, ChannelSftp sftp) {
boolean bcreated = false;
boolean bparent = false;
File file = new File(filepath);
String ppath = file.getParent();
try {
this.sftp.cd(ppath);
bparent = true;
} catch (SftpException e1) {
bparent = false;
}
try {
if (bparent) {
try {
this.sftp.cd(filepath);
bcreated = true;
} catch (Exception e) {
bcreated = false;
}
if (!bcreated) {
this.sftp.mkdir(filepath);
bcreated = true;
}
return;
} else {
createDir(ppath, sftp);
this.sftp.cd(ppath);
this.sftp.mkdir(filepath);
}
} catch (SftpException e) {
System.out.println("mkdir failed :" + filepath);
e.printStackTrace();
}

try {
this.sftp.cd(filepath);
} catch (SftpException e) {
e.printStackTrace();
System.out.println("can not cd into :" + filepath);
}

}

/**
* get all the files need to be upload or download
*
* @param file
* @return
*/
private List<String> getFileEntryList(String file) {
ArrayList<String> fileList = new ArrayList<String>();
InputStream in = null;
try {

in = new FileInputStream(file);
InputStreamReader inreader = new InputStreamReader(in);

LineNumberReader linreader = new LineNumberReader(inreader);
String filepath = linreader.readLine();
while (filepath != null) {
fileList.add(filepath);
filepath = linreader.readLine();
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (in != null) {
in = null;
}
}

return fileList;
}

/**
* @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;
}

/**
* @return the localPath
*/
public String getLocalPath() {
return localPath;
}

/**
* @param localPath
*            the localPath to set
*/
public void setLocalPath(String localPath) {
this.localPath = localPath;
}

/**
* @return the remotePath
*/
public String getRemotePath() {
return remotePath;
}

/**
* @param remotePath
*            the remotePath to set
*/
public void setRemotePath(String remotePath) {
this.remotePath = remotePath;
}

/**
* @return the fileListPath
*/
public String getFileListPath() {
return fileListPath;
}

/**
* @param fileListPath
*            the fileListPath to set
*/
public void setFileListPath(String fileListPath) {
this.fileListPath = fileListPath;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
FtpImpl ftp = new FtpImpl();
ftp.connect();
ftp.upload();
ftp.disconnect();
System.exit(0);
}

}
分享到:
评论

相关推荐

    SFTP上传下载文件工具

    "SFTP上传下载文件工具"通常指的是支持SFTP协议的软件应用,这些应用使得用户能够方便地在本地计算机和远程服务器之间进行文件的上传和下载。描述中提到的"可直接文件夹传输"功能,意味着这款工具不仅支持单个文件的...

    SFTP上传下载 MFC VS2010

    本项目“SFTP上传下载 MFC VS2010”专注于利用MFC(Microsoft Foundation Classes)库在Visual Studio 2010环境下实现SFTP的上传和下载功能。 MFC是微软提供的C++类库,它为Windows应用程序开发提供了丰富的接口,...

    sftp上传下载 java

    提供的压缩包文件“sftp上传下载”可能包含了实现这些功能的示例代码或更完整的解决方案,可以帮助开发者快速集成SFTP功能到他们的Java项目中。 总之,Java通过JSch库提供了强大的SFTP支持,使得在安全的环境中进行...

    android sftp上传下载

    在Android平台上实现SFTP(Secure File Transfer Protocol,安全文件传输协议)上传和下载功能,是移动应用中处理远程文件操作的常见需求。SFTP是一种基于SSH的安全文件传输协议,它提供了在不安全网络环境中安全...

    ftp上传下载,sftp上传下载

    在进行FTP或SFTP上传下载时,需要注意文件权限设置、传输速率、文件大小等因素,并确保两端系统之间的兼容性。对于大型项目,可能需要使用自动化脚本或工具来批量处理文件传输。同时,为了提高效率和可靠性,可以...

    python sftp上传下载

    python sftp上传下载

    java操作sftp上传下载

    本文将详细讲解如何使用Java实现SFTP的上传、下载以及相关的批量操作、远程目录创建和文件删除功能。 首先,我们需要一个支持SFTP的Java库,例如JSch(Java Secure Channel)。JSch是一个开放源码的Java库,它实现...

    C# SFTP上传下载文件

    以下是使用C#进行SFTP上传文件的基本步骤: 1. 引用库: ```csharp using Renci.SshNet; ``` 2. 创建SFTP客户端对象并设置连接参数: ```csharp var sshClient = new SftpClient("sftp服务器地址", "用户名",...

    pb8.0利用psftp.exe进行sftp上传下载

    本文将深入探讨如何在PowerBuilder 8.0(简称PB8.0)环境中利用psftp.exe工具进行SFTP的上传与下载操作。 **1. SFTP简介** SFTP是基于SSH(Secure Shell)协议的文件传输协议,它提供了加密的网络通信,确保数据在...

    sftp上传下载功能的实现,附带前端页面和后台代码

    综上所述,实现SFTP上传下载功能涉及到前端交互设计、后端服务器开发、文件操作、网络通信以及安全性等多个方面,需要综合运用多种技术知识。这个项目为开发者提供了一个完整的实践案例,有助于加深对相关技术的理解...

    sftp上传下载文件的Java代码

    以上就是使用Java和JSCH库进行SFTP文件上传下载的基本步骤。在实际应用中,可能还需要处理异常、重试机制、文件夹操作、权限管理等更复杂的情况。不过,这个基础示例为你提供了一个良好的起点,你可以根据需求对其...

    java实现sftp上传下载文件

    在IT行业中,Java是一种广泛应用的编程语言,尤其在企业级应用和网络服务方面。SFTP(Secure File Transfer ...在实际项目中,还可以考虑异常处理、并发上传下载、文件权限控制等高级特性,以提高系统的稳定性和效率。

    Ftp和Sftp上传下载工具类

    该工具支持ftp和sftp的上传和下载 1

    SFTp上传下载and xml解析丶生成

    首先,让我们详细了解一下SFTP上传和下载的流程。在Java中,我们可以使用开源库JSch来实现SFTP操作。JSch库提供了连接到SFTP服务器、创建会话、打开通道、传输文件等方法。上传文件通常涉及以下步骤: 1. 创建JSch...

    C# SFTP文件上传和下载,有进度条,增加多个文件文件下载

    前面上传过一次,不能编辑了,重新上传...和前面那个相比,代码优化了一下:1)上传和下载完成的时候关闭FileStream(不关闭的话下载完成之后本地操作该文件会提示被占用),2)增加了连续下载多个文件(位于Form2)。

    python sftp上传下载文件应用案例.py

    通过配置文件链接sftp服务器,下载指定文件,上传指定文件,常常用于更新服务器指定资源。

    基于qt5、sftp上传文件

    基于qt5+mingw+win7,里面包括有封装好的sftp库。参考原作者https://download.csdn.net/download/qq_26360165/10576625并修改。最简单的一个小demo

    linux脚本sftp上传文件

    linux脚本sftp上传文件

    java封装SFTP上传下载工具包

    适合java进行文件上传下载的童鞋们,常用方法已包装好,可直接使用。

    jsch-0.1.53.jar实现SFTP上传下载删除

    在本案例中,我们将关注如何利用JSch库来实现SFTP文件的上传、下载和删除功能。 **SFTP基础** SFTP是基于SSH协议的文件传输协议,它提供了安全的数据传输,确保了文件传输过程中的数据完整性。与FTP不同,SFTP在...

Global site tag (gtag.js) - Google Analytics