最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java使用JSCH實(shí)現(xiàn)SFTP文件管理

 更新時間:2019年08月16日 16:49:14   作者:默默同學(xué)  
這篇文章主要為大家詳細(xì)介紹了java使用JSCH實(shí)現(xiàn)SFTP文件管理,實(shí)現(xiàn)上傳、下載等功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java使用JSCH實(shí)現(xiàn)SFTP文件管理的具體代碼,供大家參考,具體內(nèi)容如下

一、連接配置

1.在項目中導(dǎo)入jsch-0.1.51.jar包;

2.創(chuàng)建SFTP類,存放連接屬性,其中要注意一點(diǎn),在進(jìn)行FTP操作時,一個會話在建立連接通道后進(jìn)入A目錄進(jìn)行文件操作,不能直接跳到同級或上級目錄操作,需要先退出當(dāng)前會話或者新建會話。

public class SFTP{

 private Session session;//會話 
 private Channel channel;//連接通道 
 private ChannelSftp sftp;// sftp操作類 


 public Session getSession() {
 return session;
 }
 public void setSession(Session session) {
 this.session = session;
 }
 public Channel getChannel() {
 return channel;
 }
 public void setChannel(Channel channel) {
 this.channel = channel;
 }
 public ChannelSftp getSftp() {
 return sftp;
 }
 public void setSftp(ChannelSftp sftp) {
 this.sftp = sftp;
 }


}

3.創(chuàng)建SFTPUtil類,創(chuàng)建連接配置方法

 /**
 * 連接ftp/sftp服務(wù)器
 * @param SFTP類
 */
 public static void getConnect(SFTP s) throws Exception {

 /** 密鑰的密碼 */ 
// String privateKey ="key";
// /** 密鑰文件路徑 */ 
// String passphrase ="path";
 /** 主機(jī) */ 
 String host ="127.0.0.1"; 
 /** 端口 */ 
 int port =22; 
 /** 用戶名 */ 
 String username ="test";
 /** 密碼 */ 
 String password ="test";

 Session session = null; 
 Channel channel = null; 
 ChannelSftp sftp = null;// sftp操作類 

 JSch jsch = new JSch(); 


 //設(shè)置密鑰和密碼 
 //支持密鑰的方式登陸,只需在jsch.getSession之前設(shè)置一下密鑰的相關(guān)信息就可以了 
// if (privateKey != null && !"".equals(privateKey)) { 
// if (passphrase != null && "".equals(passphrase)) { 
// //設(shè)置帶口令的密鑰 
//  jsch.addIdentity(privateKey, passphrase); 
// } else { 
// //設(shè)置不帶口令的密鑰 
//  jsch.addIdentity(privateKey); 
// } 
// } 
 session = jsch.getSession(username, host, port); 
 session.setPassword(password); 
 Properties config = new Properties(); 
 config.put("StrictHostKeyChecking", "no"); // 不驗(yàn)證 HostKey 
 session.setConfig(config); 
 try {
 session.connect(); 
 } catch (Exception e) {
 if (session.isConnected()) 
 session.disconnect(); 
 log.error("連接服務(wù)器失敗,請檢查主機(jī)[" + host + "],端口[" + port 
  + "],用戶名[" + username + "],端口[" + port 
  + "]是否正確,以上信息正確的情況下請檢查網(wǎng)絡(luò)連接是否正?;蛘哒埱蟊环阑饓芙^."); 
 }
 channel = session.openChannel("sftp"); 
 try {
 channel.connect(); 
 } catch (Exception e) { 
 if (channel.isConnected()) 
 channel.disconnect(); 
 log.error("連接服務(wù)器失敗,請檢查主機(jī)[" + host + "],端口[" + port 
  + "],用戶名[" + username + "],密碼是否正確,以上信息正確的情況下請檢查網(wǎng)絡(luò)連接是否正?;蛘哒埱蟊环阑饓芙^."); 
 }
 sftp = (ChannelSftp) channel; 

 s.setChannel(channel);
 s.setSession(session);
 s.setSftp(sftp);

 }

5.關(guān)閉連接方法

 /**
 * 斷開連接
 * 
 */
 public static void disConn(Session session,Channel channel,ChannelSftp sftp)throws Exception{
 if(null != sftp){
 sftp.disconnect();
 sftp.exit();
 sftp = null;
 }
 if(null != channel){
 channel.disconnect();
 channel = null;
 }
 if(null != session){
 session.disconnect();
 session = null;
 }
 }

二、SFTP目錄、文件操作管理

1.上傳文件

 /**
 * 上傳文件
 * @param directory 上傳的目錄-相對于SFPT設(shè)置的用戶訪問目錄,
 * 為空則在SFTP設(shè)置的根目錄進(jìn)行創(chuàng)建文件(除設(shè)置了服務(wù)器全磁盤訪問)
 * @param uploadFile 要上傳的文件全路徑
 */
 public static void upload(String directory,String uploadFile) throws Exception {

 SFTP s=new SFTP();
 getConnect(s);//建立連接
 Session session = s.getSession(); 
 Channel channel = s.getChannel(); 
 ChannelSftp sftp = s.getSftp();// sftp操作類 
 try {
 try{
  sftp.cd(directory); //進(jìn)入目錄
 }catch(SftpException sException){
  if(sftp.SSH_FX_NO_SUCH_FILE == sException.id){ //指定上傳路徑不存在
  sftp.mkdir(directory);//創(chuàng)建目錄
  sftp.cd(directory); //進(jìn)入目錄
  }
 }


 }
 File file = new File(uploadFile);
 InputStream in= new FileInputStream(file);
 sftp.put(in, file.getName());
 in.close();

 } catch (Exception e) {
 throw new Exception(e.getMessage(),e); 
 } finally {
 disConn(session,channel,sftp);
 }
 }

2.文件下載

/**
 * 下載文件
 * @param directory 下載目錄 根據(jù)SFTP設(shè)置的根目錄來進(jìn)行傳入
 * @param downloadFile 下載的文件 
 * @param saveFile 存在本地的路徑 
 */
 public static void download(String directory, String downloadFile,String saveFile) throws Exception {
 SFTP s=new SFTP();
 getConnect(s);//建立連接
 Session session = s.getSession(); 
 Channel channel = s.getChannel(); 
 ChannelSftp sftp = s.getSftp();// sftp操作類 
 try {

 sftp.cd(directory); //進(jìn)入目錄
 File file = new File(saveFile);
 boolean bFile;
 bFile = false;
 bFile = file.exists();
 if (!bFile) {
 bFile = file.mkdirs();//創(chuàng)建目錄
 }
 OutputStream out=new FileOutputStream(new File(saveFile,downloadFile));

 sftp.get(downloadFile, out);

 out.flush();
 out.close();

 } catch (Exception e) {
 throw new Exception(e.getMessage(),e); 
 } finally {
 disConn(session,channel,sftp);
 }
 }

3.刪除文件

/**
 * 刪除文件
 * @param directory 要刪除文件所在目錄 
 * @param deleteFile 要刪除的文件
 */
 public static void delete(String directory, String deleteFile) throws Exception {
 SFTP s=new SFTP();
 getConnect(s);//建立連接
 Session session = s.getSession(); 
 Channel channel = s.getChannel(); 
 ChannelSftp sftp = s.getSftp();// sftp操作類 
 try {
 sftp.cd(directory); //進(jìn)入的目錄應(yīng)該是要刪除的目錄的上一級
 sftp.rm(deleteFile);//刪除目錄
 } catch (Exception e) {
 throw new Exception(e.getMessage(),e); 
 } finally {
 disConn(session,channel,sftp);
 }
 }

4.列出目錄下的文件

/** 
 * 列出目錄下的文件 
 * @param directory 要列出的目錄 
 * @return list 文件名列表 
 * @throws Exception 
 */ 
 public static List<String> listFiles(String directory) throws Exception { 
 SFTP s=new SFTP();
 getConnect(s);//建立連接
 Session session = s.getSession(); 
 Channel channel = s.getChannel(); 
 ChannelSftp sftp = s.getSftp();// sftp操作類 
 Vector fileList=null; 
 List<String> fileNameList = new ArrayList<String>(); 
 fileList = sftp.ls(directory); //返回目錄下所有文件名稱
 disConn(session,channel,sftp);

 Iterator it = fileList.iterator(); 

 while(it.hasNext()) { 

 String fileName = ((LsEntry)it.next()).getFilename(); 
 if(".".equals(fileName) || "..".equals(fileName)){ 
 continue; 
 } 
 fileNameList.add(fileName); 

 } 

 return fileNameList; 
 } 

5.刪除目錄下所有文件

/**
 * 刪除目錄下所有文件
 * @param directory 要刪除文件所在目錄 
 */
 public static void deleteAllFile(String directory) throws Exception{
 SFTP s=new SFTP();
 getConnect(s);//建立連接
 Session session = s.getSession(); 
 Channel channel = s.getChannel(); 
 ChannelSftp sftp = s.getSftp();// sftp操作類 
 try {
 List <String> files=listFiles(directory);//返回目錄下所有文件名稱
 sftp.cd(directory); //進(jìn)入目錄

 for (String deleteFile : files) {
 sftp.rm(deleteFile);//循環(huán)一次刪除目錄下的文件
 }
 } catch (Exception e) {
 throw new Exception(e.getMessage(),e); 
 } finally {
 disConn(session,channel,sftp);
 }

 }

6.刪除目錄 (刪除的目錄必須為空)

/**
 * 刪除目錄 (刪除的目錄必須為空)
 * @param deleteDir 要刪除的目錄 
 */
 public static void deleteDir(String deleteDir) throws Exception {
 SFTP s=new SFTP();
 getConnect(s);//建立連接
 Session session = s.getSession(); 
 Channel channel = s.getChannel(); 
 ChannelSftp sftp = s.getSftp();// sftp操作類 
 try {

 sftp.rmdir(deleteDir);

 } catch (Exception e) {
 throw new Exception(e.getMessage(),e); 
 } finally {
 disConn(session,channel,sftp);
 }
 }

7.創(chuàng)建目錄

 /**
 * 創(chuàng)建目錄 
 * @param directory 要創(chuàng)建的目錄 位置
 * @param dir 要創(chuàng)建的目錄 
 */
 public static void creatDir(String directory,String dir) throws Exception {
 SFTP s=new SFTP();
 getConnect(s);//建立連接
 Session session = s.getSession(); 
 Channel channel = s.getChannel(); 
 ChannelSftp sftp = s.getSftp();// sftp操作類 
 try {
 sftp.cd(directory); 
 sftp.mkdir(dir);
 } catch (Exception e) {
 throw new Exception(e.getMessage(),e); 
 } finally {
 disConn(session,channel,sftp);
 }
 }

8.更改文件名

/** 
 * 更改文件名 
 * @param directory 文件所在目錄 
 * @param oldFileNm 原文件名 
 * @param newFileNm 新文件名 
 * @throws Exception 
 */ 
 public static void rename(String directory, String oldFileNm, String newFileNm) throws Exception { 
 SFTP s=new SFTP();
 getConnect(s);//建立連接
 Session session = s.getSession(); 
 Channel channel = s.getChannel(); 
 ChannelSftp sftp = s.getSftp();// sftp操作類 
 try {
 sftp.cd(directory); 
 sftp.rename(oldFileNm, newFileNm); 
 } catch (Exception e) {
 throw new Exception(e.getMessage(),e); 
 } finally {
 disConn(session,channel,sftp);
 }
 } 

9.進(jìn)入目錄

/**
 * 進(jìn)入目錄
 * @param directory
 * @throws Exception
 */
 public static void cd(String directory)throws Exception { 

 SFTP s=new SFTP();
 getConnect(s);//建立連接
 Session session = s.getSession(); 
 Channel channel = s.getChannel(); 
 ChannelSftp sftp = s.getSftp();// sftp操作類 
 try {
 sftp.cd(directory); //目錄要一級一級進(jìn)
 } catch (Exception e) {
 throw new Exception(e.getMessage(),e); 
 } finally {
 disConn(session,channel,sftp);
 }
 } 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringData JPA實(shí)現(xiàn)查詢分頁demo

    SpringData JPA實(shí)現(xiàn)查詢分頁demo

    本篇文章主要介紹了SpringData JPA實(shí)現(xiàn)查詢分頁demo,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Java設(shè)計模式常用原則解析

    Java設(shè)計模式常用原則解析

    這篇文章主要介紹了Java設(shè)計模式常用原則解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • SpringBoot項目@Async方法問題解決方案

    SpringBoot項目@Async方法問題解決方案

    這篇文章主要介紹了SpringBoot項目@Async方法問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • Java實(shí)現(xiàn)簡單訂餐系統(tǒng)

    Java實(shí)現(xiàn)簡單訂餐系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡單訂餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • SpringBoot根據(jù)目錄結(jié)構(gòu)自動配置Url前綴方式

    SpringBoot根據(jù)目錄結(jié)構(gòu)自動配置Url前綴方式

    這篇文章主要介紹了SpringBoot根據(jù)目錄結(jié)構(gòu)自動配置Url前綴方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • RabbitMq的5種模式及實(shí)例解讀

    RabbitMq的5種模式及實(shí)例解讀

    這篇文章主要介紹了RabbitMq的5種模式及實(shí)例解讀,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Java圖片處理的簡易指南

    Java圖片處理的簡易指南

    圖像處理是各類應(yīng)用程序的重要組成部分,Java作為一種多功能且強(qiáng)大的編程語言,提供了豐富的庫和框架來高效地處理圖像處理任務(wù),本文將帶您了解Java圖像處理的基本概念、工具以及實(shí)踐示例,幫助您掌握J(rèn)ava圖像處理技術(shù),需要的朋友可以參考下
    2024-09-09
  • 淺談java中為什么實(shí)體類需要實(shí)現(xiàn)序列化

    淺談java中為什么實(shí)體類需要實(shí)現(xiàn)序列化

    下面小編就為大家?guī)硪黄獪\談java中為什么實(shí)體類需要實(shí)現(xiàn)序列化。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • @RequestBody時第二個字母大寫,映射不到的解決

    @RequestBody時第二個字母大寫,映射不到的解決

    這篇文章主要介紹了@RequestBody時第二個字母大寫,映射不到的解決方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java與php的區(qū)別淺析

    java與php的區(qū)別淺析

    在本篇文章里小編給大家整理了關(guān)于java與php的區(qū)別以及相關(guān)知識點(diǎn),有興趣的朋友們學(xué)習(xí)下。
    2019-03-03

最新評論

礼泉县| 吉木萨尔县| 牟定县| 南漳县| 南部县| 芮城县| 上林县| 崇阳县| 崇阳县| 弥渡县| 临江市| 晴隆县| 伊金霍洛旗| 新平| 修文县| 长春市| 太保市| 辽宁省| 长沙县| 玉龙| 焦作市| 卢湾区| 信宜市| 黎城县| 建湖县| 云和县| 开原市| 新乐市| 墨玉县| 电白县| 无为县| 梨树县| 左云县| 四会市| 江阴市| 易门县| 宝山区| 丹棱县| 哈尔滨市| 多伦县| 福鼎市|