分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

FTP在docker容器中上传失败解决,改为被动模式

发布时间:2023-09-06 02:00责任编辑:董明明关键词:暂无标签
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. */@Componentpublic 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(); ???????????????} ???????????} ???????} ???}}

  

FTP在docker容器中上传失败解决,改为被动模式

原文地址:https://www.cnblogs.com/amoyzhu/p/9197500.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved