java代碼實現ftp服務器的文件上傳和下載


java代碼實現文件上傳到ftp服務器:

1:ftp服務器安裝:

 

 

 

2:ftp服務器的配置:

 

 

 

 

 

啟動成功:

 

 

 2:客戶端:代碼實現文件的上傳與下載:

1:依賴jar包:

 

 

 

 2:sftpTools   工具類:

 只實現了簡單的文件上傳和下載,具體業務需根據實際情況再判斷條件,使用:

  1 package com.floor.shop.util;
  2 
  3 import com.jcraft.jsch.*;
  4 import org.slf4j.LoggerFactory;
  5 
  6 import java.io.File;
  7 import java.io.FileInputStream;
  8 import java.io.FileNotFoundException;
  9 import java.io.FileOutputStream;
 10 import java.util.Properties;
 11 import java.util.Vector;
 12 
 13 public class SftpTools {
 14     private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SftpTools.class);
 15 
 16     /**
 17      * 連接sftp服務器
 18      *
 19      * @param host     主機
 20      * @param port     端口
 21      * @param username 用戶名
 22      * @param password 密碼
 23      * @return
 24      */
 25     public ChannelSftp connect(String host, int port, String username,
 26                                String password) {
 27         ChannelSftp sftp = null;
 28         try {
 29             JSch jsch = new JSch();
 30             jsch.getSession(username, host, port);
 31             Session sshSession = jsch.getSession(username, host, port);
 32             logger.info("Session created.");
 33             sshSession.setPassword(password);
 34             Properties sshConfig = new Properties();
 35             sshConfig.put("StrictHostKeyChecking", "no");
 36             sshSession.setConfig(sshConfig);
 37             sshSession.connect();
 38             logger.info("Session connected.");
 39             logger.info("Opening Channel.");
 40             Channel channel = sshSession.openChannel("sftp");
 41             channel.connect();
 42             sftp = (ChannelSftp) channel;
 43             logger.info("Connected to " + host + ".");
 44         } catch (Exception e) {
 45             logger.error(e.getMessage());
 46         }
 47         return sftp;
 48     }
 49 
 50     /*
 51      *測試文件上傳、下載:
 52      * host:ftp服務器的IP地址;
 53      * port:ftp服務器的端口號;
 54      * username:ftp服務器的用戶名;
 55      * password:ftp服務器的密碼;
 56      * fileUrl:文件的上傳路徑;
 57      */
 58     //ftp文件上傳:
 59     public static void fileUpload(String fileUrl,String host,int port,String username,String password)
 60             throws FileNotFoundException {
 61         SftpTools sftpTools = new SftpTools();
 62         //連接ftp服務器:
 63         ChannelSftp connect = sftpTools.connect(host, port, username, password);
 64         //創建需要上傳的文件對象: (fileUrl:上傳文件的路徑)
 65         File file = new File(fileUrl);
 66         try {
 67             connect.put(new FileInputStream(file), file.getName());
 68         } catch (SftpException e) {
 69             e.printStackTrace();
 70         }
 71     }
 72     /*
 73      *ftp文件下載:
 74      * saveUrl:保存文件的路徑;
 75      * saveName:保存文件的名字;
 76      * downloadFileName:下載的文件名,需要加上后綴;
 77      */
 78     public static void fileDownLoad(String saveUrl,String saveName,String host,int port,
 79                                     String username,String password,String downloadFileName) {
 80         SftpTools sftpTools = new SftpTools();
 81         //調用連接ftp的方法:
 82         ChannelSftp connect = sftpTools.connect(host, port, username, password);
 83         try {
 84             //獲取ftp服務器存放或讀取文件的目錄:
 85             String files = connect.pwd();
 86             //進入服務器資源路徑:
 87             connect.cd(files);
 88             //創建文件保存路徑的對象:(保存的路徑:saveUrl;自定義保存的文件名:saveName)
 89             File file = new File(saveUrl+"\\"+saveName);
 90             try {
 91                 //下載的文件名:test.jpg
 92                 connect.get(downloadFileName, new FileOutputStream(file));
 93             } catch (FileNotFoundException e) {
 94                 e.printStackTrace();
 95             }
 96         } catch (SftpException e) {
 97             e.printStackTrace();
 98         }
 99     }
100 
101 
102     /**
103      * 刪除文件
104      *
105      * @param directory  要刪除文件所在目錄
106      * @param deleteFile 要刪除的文件
107      */
108     public static void delete(String directory, String deleteFile,String host,int port,String username,
109                               String password) {
110         SftpTools sftpTools = new SftpTools();
111         //連接ftp服務器:
112         ChannelSftp sftp = sftpTools.connect(host, port, username, password);
113         try {
114             sftp.cd(directory);
115             sftp.rm(deleteFile);
116         } catch (Exception e) {
117             e.printStackTrace();
118         }
119     }
120 
121     /**
122      * 檢查文件夾是否存在
123      *
124      * @param dirPath 文件路徑:完整
125      * @return true :存在; false: 不存在
126      * @throws SftpException
127      */
128     public static String checkDir(String dirPath,String host,int port, String username,  String password) {
129         SftpTools sftpTools = new SftpTools();
130         //連接ftp服務器:
131         ChannelSftp sftp = sftpTools.connect(host, port, username, password);
132         String exists = "";
133         try {
134             String files = sftp.pwd();
135             if (files.contains(dirPath)) {
136                 return "true";
137             }
138             //判斷日期目錄
139             Vector content = sftp.ls(dirPath);
140             if (content == null) {
141                 exists = "false";
142             } else {
143                 exists = "true";
144             }
145         } catch (Exception e) {
146             exists = "false";
147             logger.error("判斷日期目錄出錯: " + e.getMessage());
148         }
149         return exists;
150 
151     }
152 
153     //檢查文件夾是否存在,不存在自動創建文件夾:
154     public static String checkDirMore(String dirPath,String host,int port, String username,String password) {
155         //檢查//upload目錄是否存在
156         String isDir = checkDir(dirPath,host,port,username,password);
157         if (isDir.equals("true")) {
158             return "true";
159         } else {
160             boolean isMake = makeDir(dirPath,host,port,username,password);
161             if (isMake == true) {
162                 return "true";
163             } else {
164                 return "false";
165             }
166         }
167     }
168 
169     //創建目錄:
170     public static boolean makeDir(String dirPath,String host,int port, String username,String password) {
171         SftpTools sftpTools = new SftpTools();
172         //連接ftp服務器:
173         ChannelSftp sftp = sftpTools.connect(host,port,username,password);
174         try {
175             sftp.mkdir(dirPath);
176             return true;
177         } catch (SftpException e) {
178             e.printStackTrace();
179             logger.error("創建目錄失敗: " + dirPath);
180             return false;
181         }
182     }
183 }
View Code

 

 

 

詳解可參考:http://www.cnblogs.com/longyg/archive/2012/06/25/2561332.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM