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

原文地址:https://www.cnblogs.com/dw3306/p/9429090.html