在网上找了一会发现一个兄弟写的帮助了我
https://blog.csdn.net/LNView/article/details/72412606
1、添加对应的maven依赖
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.51</version> </dependency> <!-- jsch 所依赖的jar包 --> <dependency> <groupId>com.jcraft</groupId> <artifactId>jzlib</artifactId> <version>1.0.7</version> </dependency>
2、在.properties进行配置ftp的对应信息
#服务器IP地址 ftp.host= 127.0.0.1 #ftp端口号 ftp.port=22 #ftp用户名 ftp.name=test #ftp密码 ftp.password=test #ftp上传文件目录 ftp.directory=/app/img #文件下载保存目录 ftp.saveFile=
3、通过@Value进行获取对应的ftp服务信息
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * @author zhouzenghui * @title: Ftp * @description: ftp信息;类 * @date 2019/7/10 17:58 */ @Component public class Ftp { public static String host; public static int port; public static String user; public static String password; public static String directory; public static String saveFile; @Value("${ftp.host}") public void setHost(String host) { Ftp.host = host; } @Value("${ftp.port}") public void setPort(int port) { Ftp.port = port; } @Value("${ftp.name}") public void setUser(String user) { Ftp.user = user; } @Value("${ftp.password}") public void setPassword(String password) { Ftp.password = password; } @Value("${ftp.directory}") public void setDirectory(String directory) { Ftp.directory = directory; } @Value("${ftp.saveFile}") public void setSaveFile(String saveFile) { Ftp.saveFile = saveFile; } }
4、进行对应的实现
import com.jcraft.jsch.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; import java.util.Vector; /** * @author zhouzenghui * @title: FtpJSch * @description: TODO * @date 2019/7/10 15:11 */ public class FtpJSch { private final static Logger logger = LoggerFactory.getLogger(FtpJSch.class); private static ChannelSftp sftp = null; private static Session sshSession = null; /** * 建立连接 * * @return */ public static FtpJSch getConnect() { FtpJSch ftp = new FtpJSch(); try { JSch jsch = new JSch(); //获取sshSession 账号-ip-端口 sshSession = jsch.getSession(Ftp.user, Ftp.host, Ftp.port); //添加密码 sshSession.setPassword(Ftp.password); Properties sshConfig = new Properties(); //严格主机密钥检查 sshConfig.put("StrictHostKeyChecking", "no"); sshSession.setConfig(sshConfig); //开启sshSession链接 sshSession.connect(); //获取sftp通道 Channel channel = sshSession.openChannel("sftp"); //开启 channel.connect(); sftp = (ChannelSftp) channel; } catch (Exception e) { e.printStackTrace(); } return ftp; } /** * 断开连接 */ public static void disconnect() { //断开sftp连接 if (sftp != null) { sftp.disconnect(); logger.debug("SSH Channel disconnected.channel={}", sftp); } //断开sftp连接之后,再断开session连接 if (sshSession != null) { sshSession.disconnect(); logger.debug("SSH session disconnected.session={}", sshSession); } } /** * 上传文件 * * @param uploadFile 上传文件的路径 * @return 服务器上文件名 */ public static String upload(String uploadFile) { File file = null; String fileName = null; try { sftp.cd(Ftp.directory); file = new File(uploadFile); //获取原文件名 fileName = file.getName(); //调用ftp进行上传 sftp.put(new FileInputStream(file), fileName); } catch (Exception e) { e.printStackTrace(); } return file == null ? null : fileName; } /** * 下载文件 * * @param downloadFileName 下载文件名 */ public void download(String downloadFileName) { try { sftp.cd(Ftp.directory); File file = new File(Ftp.saveFile); sftp.get(downloadFileName, new FileOutputStream(file)); } catch (Exception e) { e.printStackTrace(); } } /** * 删除文件 * * @param deleteFile 要删除的文件名字 */ public void delete(String deleteFile) { try { sftp.cd(Ftp.directory); sftp.rm(deleteFile); } catch (Exception e) { e.printStackTrace(); } } /** * 列出目录下的文件 * * @param directory 要列出的目录 * @return * @throws SftpException */ public Vector listFiles(String directory) throws SftpException { return sftp.ls(directory); } // public static void main(String[] args) { // FtpJSch.getConnect(); // String filename = FtpJSch.upload("D:\\22.pdf"); // disconnect(); // System.out.println(filename); // } }
5、测试
@Test public void contextLoads() { System.out.println(this.host); FtpJSch.getConnect(); String filename = FtpJSch.upload("D:\\22.pdf"); FtpJSch.disconnect(); }
测试结果