sftp 的使用
发布于: 1 小时前
sfp 使用的是 22 端口。在 linux 机器上安装 sftp 以后,设计置用户组和用户,以及权限目录。
windows 中可以使用 xftp 客户端去远程连接服务器。
linux 中可以使用 sftp 的命令去远程连接远程服务器。
在 java 中使用 sftp:
上传,以流的形式。
/** * 上传到SFTP服务器上文件的路径 * * @param ftpPath * @param fileName * @param is * @return * @throws JSchException */public boolean uploadFile(String ftpPath, String fileName, InputStream is, String cycleId) throws JSchException, IOException { List<Object> list_obj = getConnect(); Session session = (Session) list_obj.get(0); Channel channel = (Channel) list_obj.get(1); channel.connect(); ChannelSftp chSftp = (ChannelSftp) channel; try { chSftp.cd(ftpPath); SftpATTRS attrs = null; try { attrs = chSftp.stat(cycleId); } catch (Exception e) { } if (attrs == null) { chSftp.mkdir(cycleId); logger.info("创建子目录:" + cycleId); } chSftp.cd(cycleId); chSftp.put(is, fileName); return true; } catch (Exception e) { e.printStackTrace(); logger.info("up error."); } finally { is.close(); chSftp.quit(); channel.disconnect(); session.disconnect(); } return false;}复制代码
上传,直接上传文件。
/** * @param ftpPath 上传到SFTP服务器上文件的路径 * @param ZipFilePath 本地的位置 * @param fileName 文件名称 上传文件到ftp指定目录 * @throws JSchException */ public boolean upDataFile(String ftpPath, String ZipFilePath, String fileName, String cycleId) throws JSchException { List<Object> list_obj = getConnect(); Session session = (Session) list_obj.get(0); Channel channel = (Channel) list_obj.get(1); channel.connect(); ChannelSftp chSftp = (ChannelSftp) channel;// String ftpFilePath = ftpPath + fileName; String localFilePath = ZipFilePath + fileName; try { chSftp.cd(ftpPath); SftpATTRS attrs = null; try { attrs = chSftp.stat(cycleId); } catch (Exception e) { } if (attrs == null) { chSftp.mkdir(cycleId); logger.info("创建子目录:" + cycleId); } chSftp.cd(cycleId); chSftp.put(localFilePath, fileName); return true; } catch (Exception e) { e.printStackTrace(); logger.info("up error."); } finally { chSftp.quit(); channel.disconnect(); session.disconnect(); } return false; }复制代码
下载,流的形式。
/** * 获取sftp文件流 * * @param ftpPath * @param fileName * @return * @throws JSchException * @throws IOException */ public InputStream downloadSftpFile(String ftpPath, String fileName) throws JSchException, IOException { InputStream is = null; List<Object> list_obj = getConnect();// Session session = (Session)list_obj.get(0); Channel channel = (Channel) list_obj.get(1); channel.connect(); ChannelSftp chSftp = (ChannelSftp) channel; String ftpFilePath = ftpPath + fileName; try { is = chSftp.get(ftpFilePath); } catch (Exception e) { e.printStackTrace(); logger.info("download error."); if (is != null) { is.close(); } } finally {// chSftp.quit();// channel.disconnect();// session.disconnect(); } return is; }复制代码
下载,文件的方式。
/* * 从SFTP服务器下载文件 * * @param ftpPath SFTP服务器中文件所在路径 * * @param localPath 下载到本地的位置 * * @param fileName 文件名称 */ public boolean downloadSftpFile(String ftpPath, String localPath, String fileName) throws JSchException {// if(ftpHost==null){// ftpHost=ftphost;// }// if(ftpUserName==null){// ftpUserName =ftpuser;// }// if(ftpPassword==null){// ftpPassword =ftppass;// } List<Object> list_obj = getConnect(); Session session = (Session) list_obj.get(0); Channel channel = (Channel) list_obj.get(1); channel.connect(); ChannelSftp chSftp = (ChannelSftp) channel; String ftpFilePath = ftpPath + fileName; File localPathFile = new File(localPath); localPathFile.mkdirs(); String localFilePath = localPath+ fileName; try { chSftp.get(ftpFilePath, localFilePath); return true; } catch (Exception e) { e.printStackTrace(); logger.info("download error."); } finally { chSftp.quit(); channel.disconnect(); session.disconnect(); } return false; }复制代码
获取文件名
/** * @param ftpPath SFTP服务器上文件的目录 拿到指定目录下的所有文件名 * @throws JSchException */ @SuppressWarnings("unchecked") public List<String> getFileName(String ftpPath) throws JSchException { List<Object> list_obj = getConnect(); Session session = (Session) list_obj.get(0); Channel channel = (Channel) list_obj.get(1); channel.connect(); ChannelSftp chSftp = (ChannelSftp) channel; try { Vector<LsEntry> v = chSftp.ls(ftpPath); List<String> list = new ArrayList<>(); for (LsEntry obj : v) { String fileName = obj.getFilename(); list.add(fileName);
} return list; } catch (Exception e) { e.printStackTrace(); logger.info("getFileName error.");
} finally { chSftp.quit(); channel.disconnect(); session.disconnect(); }
return null; }复制代码
删除文件
/** * 删除指定路径文件 * * @param path * @return * @throws JSchException */public boolean removeFile(String path) throws JSchException { List<Object> list_obj = getConnect(); Session session = (Session) list_obj.get(0); Channel channel = (Channel) list_obj.get(1); channel.connect(); ChannelSftp chSftp = (ChannelSftp) channel; try { chSftp.rm(path); return true; } catch (Exception e) { e.printStackTrace(); logger.info("ls error."); } finally { chSftp.quit(); channel.disconnect(); session.disconnect(); } return false;}
复制代码
创建目录
/** * 创建目录 * * @param path * @return * @throws JSchException */public boolean mkDir(String path) throws JSchException { List<Object> list_obj = getConnect(); Session session = (Session) list_obj.get(0); Channel channel = (Channel) list_obj.get(1); channel.connect(); ChannelSftp chSftp = (ChannelSftp) channel; try { chSftp.mkdir(path); ; return true; } catch (Exception e) { e.printStackTrace(); logger.info("ls error."); } finally { chSftp.quit(); channel.disconnect(); session.disconnect(); } return false;}
复制代码
删除目录
/** * 删除目录 * * @param path * @return * @throws JSchException */public boolean rmDir(String path) throws JSchException { List<Object> list_obj = getConnect(); Session session = (Session) list_obj.get(0); Channel channel = (Channel) list_obj.get(1); channel.connect(); ChannelSftp chSftp = (ChannelSftp) channel; try { chSftp.rmdir(path); ; return true; } catch (Exception e) { e.printStackTrace(); logger.info("ls error."); } finally { chSftp.quit(); channel.disconnect(); session.disconnect(); } return false;}复制代码
划线
评论
复制
发布于: 1 小时前阅读数: 2
版权声明: 本文为 InfoQ 作者【拾贝】的原创文章。
原文链接:【http://xie.infoq.cn/article/add6473470b1b49b9daaccd8a】。文章转载请联系作者。
拾贝
关注
还未添加个人签名 2019.05.10 加入
还未添加个人简介











评论