FTP在docker容器中上传失败解决,改为被动模式
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();
FTP在docker容器中上传失败解决,改为被动模式的更多相关文章
- Struts2文件上传方式与上传失败解决方式
首先将几个对象弄出来第一个 上传页面第二个 上传action第三个 startut2配置文件 我的文字描述不是很好,但是终归是自己写出来的,后来我在网上看到一篇关于文件上传描述的非常清楚的文章, 链接 ...
- 连接docker里面的mysql失败解决
场景:在虚拟机的docker容器中安装latest版本的mysql之后,在宿主机中使用navicat连接虚拟机中的mysql出现下图报错: 解决方法: 1.首先docker ps命令查看正在运行的容器 ...
- redis 连接 docker容器 6379端口失败
redis部署在docker容器中Could not connect to Redis 容器内redis-cli是可以直接连上的,但是在另一台服务器上就不能用外网ip来连了 虽然我创建redis容器时 ...
- docker学习笔记(1)——ubuntu16.04安装docker(含如何彻底卸载docker,docker拉取镜像失败解决)
参考博客: 1.官网教程:https://docs.docker.com/engine/install/ 根据本机不同的信息选择不同的安装方式: https://docs.docker.com/ ...
- uploadify上传文件Firefox浏览器上传失败解决方法
近期做文件上传使用到了uploadify 可是出现了各种奇葩的问题.并且针对各个不同浏览器问题不同 在Firefox中.非常坑爹的是.每次上传就丢失session值,可是我的系统在登录.保存文件文件夹 ...
- docker 容器terminal失败
关键一句话:docker 容器的teminal失败,一定是等待资源导致的,不管是pid资源,还是内存资源.本文主要讲因为内存资源导致进程D状态,然后导致teminal容器失败. 目前在集群中,cpu占 ...
- docker容器中解决出现:^H^H^H^H
docker容器中解决出现:^H^H^H^H 环境:docker容器是debain系统 解决: 把stty erase ^H 添加到.bash_profile中 vim /etc/profile st ...
- js上传文件过大导致上传失败原因以及解决办法
背景:项目需要用到上传视频功能,由于视频有知识产权,要求必须上传到自己的服务器上不允许用第三方视频网站接口上传,于是一开始开始用的是input type=file去上传,小的视频上传没有问题,上传将近 ...
- KindEditor文件上传成功前端显示上传失败
一.使用kindeditor 上传图片 ,根据kindeditor 要求返回了相应的数据, 但是kindeditor 插件显示上传失败!!! 解决方法: 各个版本位置可能不同!!! 1.修改kinde ...
随机推荐
- How to Get Vertical Line from Point and Line
Description How to get vertical line cross one point which out of line in line. QPoint Line::Vertica ...
- 监测IIS上网站的连接数
1.运行,输入,perfmon.msc 2.性能监视器>右侧区域>右键 添加计数器 3.在“可用计数器” 区域,选择Web Service,展开Web Service,选择Current ...
- Python深度学习之安装theano(windows)
前方预警:windows的坑太多了,抛弃用linux吧 安装theano,提前清空自己的python环境吧,坑太多了,anaconda会自动安装path 一,首先安装python包管理anaconda ...
- python多线程编程5: 条件变量同步-乾颐堂
互斥锁是最简单的线程同步机制,Python提供的Condition对象提供了对复杂线程同步问题的支持.Condition被称为条件变量,除了提供与Lock类似的acquire和release方法外,还 ...
- 分布式理论系列(三)ZAB 协议
分布式理论系列(三)ZAB 协议 在学习了 Paxos 后,接下来学习 Paxos 在开源软件 Zookeeper 中的应用. 一.Zookeeper Zookeeper 致力于提供一个高性能.高可用 ...
- 07Mendel's First Law
Problem Figure 2. The probability of any outcome (leaf) in a probability tree diagram is given by th ...
- windows平台下spark-shell配置
一.下载安装spark,http://spark.apache.org/,选择合适版本后下载到本地,解压,bin目录下spark-shell文件就是spark命令行交互的入口. 二.下载安装windo ...
- 基于swoole搭建聊天室程序
1. 创建websocket服务器 swoole从1.7.9版本开始, 内置了websocket服务器功能,我们只需几行简单的PHP代码,就可以创建出一个异步非阻塞多进程的WebSocket服务器. ...
- Shell脚本传递带有空格的参数[摘录自网络]
参数处理 说明 $# 传递到脚本的参数个数 $* 以一个单字符串显示所有向脚本传递的参数 $$ 脚本运行的当前进程ID号 $! 后台运行的最后一个进程的ID号 $@ 与$#相同,但是使用时加引号,并在 ...
- vc++ 不同对话框中传递信息的方法(基于自定义消息SendMessage) (转载)
转载自:http://blog.csdn.net/myj0513/article/details/6827360 背景: 新建了一个基于对话框的MFC程序,在主对话框中添加tabcontrol控件,又 ...