admin 管理员组

文章数量: 1184232


2024年2月25日发(作者:url中文乱码问题)

sftp文件上传和下载

sftp是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的加密方法。sftp 与 ftp 有着几乎一样的语法和功能。SFTP 为 SSH的一部份,是一种传输档案至 Blogger 伺服器的安全方式。其实在SSH软件包中,已经包含了一个叫作SFTP(Secure File Transfer Protocol)的安全文件传输子系统,SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接操作,所以从某种意义上来说,SFTP并不像一个服务器程序,而更像是一个客户端程序。SFTP同样是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。

下面是java 代码sftp文件上传和下载具体实现

1.连接服务器类

package Test;

import ption;

import tream;

import ties;

import l;

import lSftp;

import ;

import n;

/*

* @author lixiongjun

*

* 利用JSch包实现创建ChannelSftp通信对象的工具类

* 2015-1-27 上午10:03:21

*/

public class SftpUtil {

private static String ip = null; // ip 主机IP

private static int port = -1; // port 主机ssh2登陆端口,如果取默认值,传-1

private static String user = null; // user 主机登陆用户名

private static String psw = null; // psw 主机登陆密码

private static String servicePath = null; // 服务存储文件路径

private static Properties prop = null;

private static Session session = null;

private static Channel channel = null;

// 读取配置文件的参数

static {

prop = new Properties();

ClassLoader cl = ssLoader();

InputStream is = cl

.getResourceAsStream("Test/Sftp_ties");

try {

(is);

} catch (IOException e) {

// n("读取配置文件出错!");

tackTrace();

}

ip = perty("ip");

port = nt(perty("port"));

user = perty("user");

psw = perty("psw");

servicePath = perty("servicePath");

}

/**

* 获取 sftp通信

*

* @return 获取到的ChannelSftp

* @throws Exception

*/

public static ChannelSftp getSftp() throws Exception {

ChannelSftp sftp = null;

JSch jsch = new JSch();

if (port <= 0) {

// 连接服务器,采用默认端口

session = sion(user, ip);

} else {

// 采用指定的端口连接服务器

session = sion(user, ip, port);

}

// 如果服务器连接不上,则抛出异常

if (session == null) {

throw new Exception("session is null");

}

// 设置登陆主机的密码

sword(psw);// 设置密码

// 设置第一次登陆的时候提示,可选值:(ask | yes | no)

fig("StrictHostKeyChecking", "no");

// 设置登陆超时时间

t(30000);

try {

// 创建sftp通信通道

channel = (Channel) annel("sftp");

t();

sftp = (ChannelSftp) channel;

// 进入服务器指定的文件夹

(servicePath);

} catch (Exception e) {

tackTrace();

}

return sftp;

}

/**

* 关闭连接

*/

public static void closeSftp() {

if (channel != null) {

if (ected())

nect();

}

if (session != null) {

if (ected())

nect();

}

}

}

2.上传下载工具类

package Test;

import edReader;

import edWriter;

import putStream;

import tputStream;

import ption;

import tream;

import treamReader;

import Stream;

import ;

import lSftp;

public class SftpUploadOrDownload {

/*

* @author lixiongjun

*

* 实现sftp文件上传下载的工具类

* 2015-1-27 下午15:13:26

*/

/**

* 上传本地文件到服务器

* @param srcPath 本地文件路径

* @param dstFilename 目标文件名

* @return 是否上传成功

*/

public boolean upload(String srcPath,String dstFilename){

ChannelSftp sftp=null;

try {

sftp =p();

(srcPath,dstFilename);

return true;

} catch (Exception e) {

tackTrace();

return false;

}

finally{

ftp();

}

}

/**

* 上传本地文件到服务器

* @param in 输入流

* @param dstFilename 目标文件名

* @return 是否上传成功

*/

public boolean uploadBystrem(InputStream in,String dstFilename){

ChannelSftp sftp=null;

try {

sftp =p();

(in,dstFilename);

return true;

} catch (Exception e) {

tackTrace();

return false;

}

finally{

ftp();

}

}

/**

* 把服务器文件下载到本地

* @param desPath 下载到本地的目标路径

* @param srcFilename 源文件名

* @return 是否下载成功

*/

public boolean download(String srcFilename,String dstPath){

ChannelSftp sftp=null;

try {

sftp =p();

(srcFilename,dstPath);

return true;

} catch (Exception e) {

tackTrace();

return false;

}

finally{

ftp();

}

}

/**

* 获取服务器文件的内容,只对文本文件有效

* @param srcFilename 服务器所在源文件名

* @return 文件内容

*/

public String getServiceFileContext(String srcFilename){

ChannelSftp sftp=null;

InputStream in=null;

BufferedReader br=null;

String filecontext="";

try {

sftp =p();

in=(srcFilename);

br=new BufferedReader(new InputStreamReader(in));

String str=ne();

while(str!=null){

filecontext+=str+"n";

str=ne();

}

} catch (Exception e) {

tackTrace();

}

finally{

try {

();

();

} catch (IOException e) {

tackTrace();

}

ftp();

}

return filecontext;

}

/**

* 删除服务器上文件

* @param filename 要删除的文件名

* @return 是否删除成功

*/

public boolean remove(String filename){

ChannelSftp sftp=null;

try {

sftp =p();

(filename);

return true;

} catch (Exception e) {

tackTrace();

return false;

}

finally{

ftp();

}

}

@Test

public void TestSftpUpload(){

if(upload("E:/","")){

n("上传文件成功");

}

else{

n("上传文件失败");

}

}

@Test

public void TestSftpDownload(){

if(download("","E:/")){

n("下载文件成功");

}

else{

n("下载文件失败");

}

}

@Test

public void TestSftpgetServiceFileContext(){

String context=getServiceFileContext("");

n(context);

}

@Test

public void TestSftpRemove(){

if(remove("")){

n("删除文件成功");

}

else{

n("删除文件失败");

}

}

}

配置文件 Sftp_ties

ip=127.0.0.1

port=22

user=test

psw=test

servicePath=testpath

下载的get方法详情 API /rangqiwei/article/details/9002046

上传的put方法详情 API /longyg/archive/2012/06/25/


本文标签: 文件 服务器 连接 上传 传输