java实现SFTP上传、下载文件功能

 时间:2024-10-12 02:55:21

1、 【连接SFTP】:首先创建sftp对象,提供了基于密码认证的衡痕贤伎sftp对象 、基于秘钥认证的sftp对象两种方式;创建了sftp对象之后,就是登录sftp。sftp对象、登录的实现方法如下图:

java实现SFTP上传、下载文件功能
java实现SFTP上传、下载文件功能

2、 【文件上传】:文件上传需要提供服务器的基础路径 、上传的目录、sftp端文件名 及文件流,系统会将文件上传至指定目录,实现代码如下:

java实现SFTP上传、下载文件功能

3、 【下载文件】:下载文件需要提供服务器的下载的目录、sftp端文件名 及本地路径,系统会将文件下载到指定目录,实现代码如下:

java实现SFTP上传、下载文件功能

4、 【删除文件】:删除文件需要提供服务器的下载的目录、sftp端文件名,系统会将指定文件删除,实现代码如下:

java实现SFTP上传、下载文件功能

5、 【关闭连接】:在做完上传、下载等操作后,需要将连接关闭。关闭连接的实现代码如下:

java实现SFTP上传、下载文件功能

6、 【测试类】:可以写一个main方法了测试下各个方法的正确性,测试类如下图所示:

java实现SFTP上传、下载文件功能

7、 【源代码】:package com.mdcl.bjcnc.ituserExam;import java.util.Properties;import java.io.File; import org.apache.commons.io.IOUtils; import java.io.FileInputStream;import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Vector;import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; public class SFTPUtil{ private String Type = "sftp"; private ChannelSftp sftp; private Session session; private String username;//登录用户名 private String password;//登录密码 private String privateKey;//私钥 private String host;//服务器地址IP地址 private int port;//端口 /** * 构造基于密码认证的sftp对象 */ public SFTPUtil(String username, String password, String host, int port) {//port=22 this.username = username; this.password = password; this.host = host; this.port = port; } /** * 构造基于秘钥认证的sftp对象 */ public SFTPUtil(String username, String host, int port, String privateKey) { this.username = username; this.host = host; this.port = port; this.privateKey = privateKey; } public SFTPUtil(){ } /** * 连接sftp服务器 */ public void login(){ try { JSch jsch = new JSch(); if (privateKey != null) { jsch.addIdentity(privateKey);// 设置私钥 } session = jsch.getSession(username, host, port); if (password != null) { session.setPassword(password); } session.setTimeout(100000); Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); Channel channel = session.openChannel(Type); channel.connect(); sftp = (ChannelSftp) channel; } catch (JSchException e) { e.printStackTrace(); } } /** * 关闭连接 server */ public void logout(){ if (sftp != null) { if (sftp.isConnected()) { sftp.disconnect(); } } if (session != null) { if (session.isConnected()) { session.disconnect(); } } } /** * 将输入流的数据上传到sftp作为文件。文件完整路径=basePath+directory * @param basePath 服务器的基础路径 * @param directory 上传到该目录 * @param sftpFileName sftp端文件名 * @param in 输入流 */ public void upload(String basePath,String directory, String sftpFileName, InputStream input) throws SftpException{ try { sftp.cd(basePath); sftp.cd(directory); } catch (SftpException e) { //目录不存在,则创建文件夹 String [] dirs=directory.split("/"); String tempPath=basePath; for(String dir:dirs){ if(null== dir || "".equals(dir)) continue; tempPath+="/"+dir; try{ sftp.cd(tempPath); }catch(SftpException ex){ sftp.mkdir(tempPath); sftp.cd(tempPath); } } } sftp.put(input, sftpFileName); //上传文件 } /** * 下载文件。 * @param directory 下载目录 * @param downloadFile 下载的文件 * @param saveFile 存在本地的路径 */ public void download(String directory, String downloadFile, String saveFile) throws SftpException, FileNotFoundException{ if (directory != null && !"".equals(directory)) { sftp.cd(directory); } File file = new File(saveFile); sftp.get(downloadFile, new FileOutputStream(file)); } /** * 下载文件 * @param directory 下载目录 * @param downloadFile 下载的文件名 * @return 字节数组 */ public byte[] download(String directory, String downloadFile) throws SftpException, IOException{ if (directory != null && !"".equals(directory)) { sftp.cd(directory); } InputStream is = sftp.get(downloadFile); byte[] fileData = IOUtils.toByteArray(is); return fileData; } /** * 删除文件 * @param directory 要删除文件所在目录 * @param deleteFile 要删除的文件 */ public void delete(String directory, String deleteFile) throws SftpException{ sftp.cd(directory); sftp.rm(deleteFile); } /** * 列出目录下的文件 * @param directory 要列出的目录 * @param sftp */ public Vector<?> listFiles(String directory) throws SftpException { return sftp.ls(directory); } /** * main方法测试 * @param args * @throws FileNotFoundException * @throws SftpException * @throws JSchException * @throws SftpException * @throws FileNotFoundException */ public static void main(String[] args) throws JSchException, SftpException, FileNotFoundException { SFTPUtil sftp = new SFTPUtil("mdcl", "mdcl@1234", "10.13.71.70", 22);//2.0开发环节测试 sftp.login(); File file = new File("C:\\Users\\bjp-mk-maxm\\Desktop\\111.jpg"); InputStream is = new FileInputStream(file); System.out.println("==upload前="); sftp.upload("/u02/storage/datafile","", "test_sftp.jpg", is); System.out.println("==upload后="); sftp.logout(); }}

  • 完美解码实现硬解的设置方法
  • Windows 7操作系统设置多个IP地址
  • MacBook Air smb文件共享怎么打开
  • 虚拟化如何添加打印机
  • 为什么光纤拉内网收发器要两根纤?
  • 热门搜索
    法律伴我成长手抄报 关于圣诞节的手抄报 守法手抄报 廉洁在我心中手抄报 手抄报花边图片大全 孝行天下手抄报 手抄报感恩的心 青春手抄报图片 礼仪手抄报图片 历史手抄报内容