哲学分享:
善良智慧最突出的特点是:自己要勇于创造;
龙文浩: 感谢您的阅读,如下代码分享前,经过我千次百次的测试,没问题之后才分享出来,是对已有代码进行优化与重组。就跟诗人一样,虽然暂时未能发明汉字,大家请当这个是我的诗句,阅读一下吧。感谢您的阅读!
FTP是什么呢?
FTP是一种使用二进制或Ascii的文本传输协议简称;同时也属于基于不同操作系统的应用程序。在Window上,我们可以在IIS中新建一个FTP应用程序,然后通过虚拟路径去访问;这里需要注意的是,主动模式PORT和被动模式PASV,因为接下来我们会使用Lunix与FTP进行交互时必须使用被动模式;
被动模式工作原理解释如下:
FTP 客户端连接到FTP服务器时,通过账号密码登录成功后要进行传输时,发送PASV命令到FTP服务器, 服务器会随机开放一个端口,(端口范围在IIS中可以设置范围的),当客户端请求服务器时,服务器使用开放的端口进行数据传输。
SFTP是什么呢?
通过网络传输文件的安全文件传输协议。
由于现在大部分的Java项目都不是使用虚拟机,比如KVM,而是使用Docker容器,我们都知道我们编写的高级语言会通过编译成为镜像,然后再Lunix上运行,即运行在容器中,而工程使用容器不像虚拟机那样了,容器是运行在操作系统上的。在日常的工作中,有些同事认为文件上传到了容器之后,客户需要更换附件,同事就回复无法更换,其实,对于不是专门研究Docker的程序员来说,确实是不知道应该如何在容器中更换文件,这里怎么办呢?我是想这样子解释帮组大家理解的,您就把容器看做文件系统,其实也是文件系统,或者说将容器看做一个进程,在对外通信时,接收到文件时会将文件保存在C盘中,这样子解释,不知是否清晰。
下面的这个代码,目前是通过容器中创建了一个客户机路径,然后使用账号密码访问,进而实现FTP与SFTP的交互。
/**
* 连接sftp服务器 longwenh 2021/01/26
* @param host 主机
* @param port 端口
* @param username 用户名
* @param password 密码
* @return
*/
public ChannelSftp connect(String host, int port, String username,String password) {
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
System.out.println("Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
System.out.println("Session connected.");
System.out.println("Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("连接成功 to " + host + ".");
} catch (Exception e) {
System.out.println("连接失败 to " + e.toString() + ".");
}
return sftp;
}
/**
* 连接ftp服务器 longwenh 2021/01/26
* 获取FTPClient对象
* @param ftpHost FTP主机服务器
* @param ftpPassword FTP 登录密码
* @param ftpUserName FTP登录用户名
* @param ftpPort FTP端口 默认为21
* @return
*/
public static FTPClient getFTPClient(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword) {
FTPClient ftpClient = null;
try {
//创建一个ftp客户端
ftpClient = new FTPClient();
// 连接FTP服务器
ftpClient.connect(ftpHost, ftpPort);
// 登陆FTP服务器
ftpClient.login(ftpUserName, ftpPassword);
//设定文件流为2进制
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
System.out.println("未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
} else {
System.out.println("FTP连接成功。");
}
} catch (SocketException e) {
e.printStackTrace();
System.out.println("FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
e.printStackTrace();
System.out.println("FTP的端口错误,请正确配置。");
}
return ftpClient;
}
/**
* 指定附件地址,获取附件
* @param affAddr 附件地址
* @param fileName 文件名
* @param contractId 合同ID
*/
private static ChannelSftp sftp = null;//sftp
private static FTPClient ftp = null;//ftp
private void GetAttach(String affAddr,String fileName,String contractId) {
try {
System.out.println("在sftp的相对路径"+affAddr);
if (sftp == null)
sftp = connect("ip",22,"root","密码");//连接sftp服务器
if(ftp == null)
ftp = getFTPClient("ip",2020,"EDZ","密码");//连接ftp服务器
String downloadFile = "/richanwujinkou.doc";//文件名.txt
String directory = "/filePath";//拼接文件目录
if(downloadFile != null && !"".equals(downloadFile)) {
sftp.cd(directory);//指向目录
}
//设置本地虚拟路径 有需要放在本地的,可以使用这段代码
//File file = new File("D:\\longwenhao\\richanwujinkou1.doc");
//下载到本地
//sftp.get(downloadFile,new FileOutputStream(file));
//文件读取为InputStream字节流
InputStream is = sftp.get(downloadFile);
FileInputStream fis = null;
if (is instanceof FileInputStream) {
fis = (FileInputStream)is;
}else {
System.out.println("InputStream不能转换为java.io.FileInputStream");
}
//ftp.storeFile(downloadFile,fis);
ftp.storeFile(downloadFile,is);//上传文件
is.close();//关闭字节流
ftp.logout();
System.out.println("文件成功上传ftp");
} catch (Exception e) {
SetWriteLog(e.toString(),userName,contractId);//保存日志
System.out.println(e.toString());
} finally {
try {
ftp.disconnect();
sftp.disconnect();//关闭sftp服务
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//关闭ftp
}
}
在这里插入图片描述