FTP在docker容器中上传失败解决,改为被动模式
2024-08-28 15:56:58
package com.mayocase.takeout.utils; import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.mayocase.takeout.user.rest.UserLoginController; import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException; /**
* Created by laiwr on 2018/5/18.
*/
@Component
public class FtpUtils { private static Logger logger = LoggerFactory.getLogger(UserLoginController.class); @Autowired
private FtpConfig ftpConfig; public FTPClient ftpClient = null; /**
* 初始化链接文件
*/
public void initFtpClient() {
ftpClient = new FTPClient();
ftpClient.setRemoteVerificationEnabled(false); //取消服务器获取自身Ip地址和提交的host进行匹配,否则当不一致时会报异常。
ftpClient.setControlEncoding("utf-8"); //在连接之前设置编码类型为utf-8
try {
ftpClient.setDataTimeout(1000*120); //设置传输超时时间为120秒
ftpClient.connect(ftpConfig.getHostname(), ftpConfig.getPort()); //连接ftp服务器
ftpClient.login(ftpConfig.getUsername(), ftpConfig.getPassword()); //登录ftp服务器
int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器 if(!FTPReply.isPositiveCompletion(replyCode)){
logger.warn("【initFtpClient】: 登录服务器失败");
}
logger.warn("【initFtpClient】: 使用帐户:"+ftpConfig.getUsername()+"密码:"+ftpConfig.getPassword()+"登录ftp服务器:"+ftpConfig.getHostname()+":"+ftpConfig.getPort());
logger.warn("【initFtpClient】: 成功登录服务器,被动模式主机:"+ftpClient.getPassiveHost()+":"+ftpClient.getPassivePort());
logger.warn("【initFtpClient】: 成功登录服务器,主动模式主机:"+ftpClient.getRemoteAddress()+":"+ftpClient.getRemotePort());
logger.warn("【initFtpClient】: 成功登录服务器,本地主机:"+ftpClient.getLocalAddress()+":"+ftpClient.getLocalPort());
logger.warn("【initFtpClient】: 成功登录服务器,返回代码:"+ftpClient.getReplyCode()+",显示状态"+ftpClient.getStatus()); }catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
} /**
* 上传文件
* @param pathname ftp服务保存地址
* @param fileName 上传到ftp的文件名
* @param inputStream 输入文件流
* @return
*/
public boolean uploadFile( String pathname, String fileName,InputStream inputStream){
boolean flag = false;
try{
logger.warn("【uploadFile】: " + "开始上传文件");
initFtpClient();
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE); //设置传输的模式为二进制文件类型传输
ftpClient.makeDirectory(pathname); //设置目录
ftpClient.changeWorkingDirectory(pathname); //设置工作路径 ftpClient.enterLocalPassiveMode(); //设置被动模式(FTP客户端在docker容器内,需用被动模式)
ftpClient.storeFile(fileName, inputStream); //上传 logger.warn("【uploadFile】: " + "上传文件成功");
flag = true;
return flag;
}catch (Exception e) {
logger.warn("【uploadFile】: " + "上传文件失败");
e.printStackTrace();
return flag;
}finally{
if(null != inputStream){
try {
inputStream.close(); //关闭文件流
} catch (IOException e) {
e.printStackTrace();
}
}
if(ftpClient.isConnected()){
try{ ftpClient.logout(); //退出FTP
ftpClient.disconnect(); //断开连接
}catch(IOException e){
e.printStackTrace();
}
}
}
} }
几个步骤说明:
ftpClient.setRemoteVerificationEnabled(false);
ftpClient.setControlEncoding("utf-8");
ftpClient.connect(ftpConfig.getHostname(), ftpConfig.getPort()); //连接ftp服务器
ftpClient.login(ftpConfig.getUsername(), ftpConfig.getPassword()); //登录ftp服务器
int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器
if(!FTPReply.isPositiveCompletion(replyCode)){
logger.warn("【initFtpClient】: 登录服务器失败");
logger.warn("【initFtpClient】: connect failed...ftp服务器:");
}
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE); //设置文件类型:文件类型为二进制文件
ftpClient.setControlEncoding("GBK"); //设置编码为GBK
CreateDirecroty(pathname);
ftpClient.makeDirectory(pathname); //设置目录
ftpClient.changeWorkingDirectory(pathname); //设置工作路径 ftpClient.enterLocalPassiveMode(); //设置被动模式
ftpClient.storeFile(fileName, inputStream);//上传
inputStream.close();
ftpClient.logout();
ftpClient.disconnect();
最新文章
- ios 模拟器内部网络连接问题
- CDN 学习笔记
- Windows Azure Virtual Machine (33) Azure虚拟机删除重建
- Ubuntu Mono 运行 Helloworld
- day01-02--数据库概念介绍
- List集合 导出 Excel
- cocos2dx 3.1从零学习(一)——入门篇(一天学会打飞机)
- jquery如何让滚动条默认在最底部
- Angularjs 日期格式转换
- C# 实现磁性窗体
- linux kernel module
- STM32串口寄存器操作(转)
- ALTER TABLE SWITCH' statement failed. The table x' is partitioned while index 'x' is not partitioned.
- 浅谈 JSON.stringify 方法
- SpringBoot2.0初识
- 解析分享链接在微信内转发防封API接口的实现原理
- jQuery-mobilevalidate使用 的一些心得,小小总结
- MySQL 5.5 服务器变量详解(二)
- Haskell语言学习笔记(90)Default
- Android Studio 检查Top Activity
热门文章
- c++沉思录中 对字符串进行围边 横向连接 竖向连接操作的练习
- div模拟键盘输入
- pcd转obj
- SQL SERVER 2012数据库:开启防火墙导致外部无法连接数据库解决办法
- user_mongo_in_a_docker_and_dump_database
- [Delphi]编译条件
- Visual Studio 2017(VS2017) 企业版 Enterprise 注册码
- SQL Server创建表超出行最大限制解决方法
- 利用InfoPath实现SharePoint Server 2013列表的级联选择(Cascading Drop Down List)
- Tempdb--Allocation Bottleneck