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 ...
随机推荐
- maven项目 实现 spring mybatis 两个框架整合
1.maven项目 src main java java源文件 resources 配置文件 beans.xml spring配置文件 <?xml version="1.0" ...
- dnn ubuntu 问题
http://blog.csdn.net/moshuilangting/article/details/53926622 http://blog.csdn.net/enjoyyl/article/de ...
- oracle 查询死锁
--查询死锁 select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo.locked ...
- 斜杠反斜杠,去空格\xa0,连接函数join()
1斜杠反斜杠 斜杠:/.反斜杠:\. 反斜杠\,在windows系统中用来表示目录. 而在unix系统中,/表示目录.由于web遵循unix命名,所以在网址(URL)中,/表示目录. 在unix系统中 ...
- 如何用Python实现常见机器学习算法-3
三.BP神经网络 1.神经网络模型 首先介绍三层神经网络,如下图 输入层(input layer)有三个units(为补上的bias,通常设为1) 表示第j层的第i个激励,也称为单元unit 为第j层 ...
- android studio使用真机测试时点击Debug调试模式时报Error running app:No target device found,点击运行模式却是启动正常的
原因是adb没检测到设备(包括真机和虚拟机). 在Terminal执行adb devices命令,查看有没有连接到的设备. 如果没有设备,确认虚拟机是否正确打开,真机是否连接打开USB调试并安装驱动. ...
- 如何使用Word2016或Office365中的word撰写博客并发布
目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...
- swift po 实现动态按钮2
// // ButtonViewController.swift // PopInstall // // Created by su on 15/12/11. // Copyright © 2 ...
- WebStorm + JetBrains IDE Support 实现自动刷新功能
WebStorm 7.0 + live eidt + JetBrains IDE Support 实现自动刷新功能, WebStorm 7.0 已自带live eidt扩展 并可更改端口,WebSto ...
- ZOJ2256 Mincost 2017-04-16 19:36 44人阅读 评论(0) 收藏
Mincost Time Limit: 2 Seconds Memory Limit: 65536 KB The cost of taking a taxi in Hangzhou is n ...