最近出了一个问题就是在本地上传FTP没有一点问题 可是部署到服务器上。上传的时候总是false。解决办法

ftp.enterLocalPassiveMode();
boolean storeFile = ftp.storeFile(filename, input);
开启FTP被动传输
package com.my.blog.website.utils;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.*; /**
* JAVA FTPClient 工具类
*
* commons-net-1.4.1.jar PFTClinet jar包
*
* @author : hpp
*/
public class FtpClientUtils{ private static final Logger log = LoggerFactory.getLogger(FtpClientUtils.class); /**
* Description: 向FTP服务器上传文件
* @Version1.0
* @param url FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param path FTP服务器保存目录
* @param filename 上传到FTP服务器上的文件名
* @param input 输入流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(
String url,//FTP服务器hostname
int port,//FTP服务器端口
String username, // FTP登录账号
String password, //FTP登录密码
String path, //FTP服务器保存目录
String filename, //上传到FTP服务器上的文件名
InputStream input // 输入流
) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
log.info("FTP开始上传,文件名为:"+filename);
int reply;
ftp.connect(url, port);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
boolean login = ftp.login(username, password);//登录
if(!login)
log.info("ftp!!!!login失败");
reply = ftp.getReplyCode();
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
log.info("FTP服务器 拒绝连接");
return success;
}
boolean change = ftp.changeWorkingDirectory(path); ftp.enterLocalPassiveMode();
boolean storeFile = ftp.storeFile(filename, input);
input.close();
ftp.logout();
if(storeFile)
log.info("FTP开上传成功!文件名为:"+filename);
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
} /**
* 删除文件
* @param fileName 要删除的文件地址
* @return true/false
* @throws IOException
*/
public static boolean delete(String fileName, FTPClient ftpClient) throws IOException {
return ftpClient.deleteFile(fileName);
} /**
* 下载文件到指定目录
* @param ftpFile 文件服务器上的文件地址
* @param dstFile 输出文件的路径和名称
* @throws Exception
*/
public static void downLoad(String ftpFile, String dstFile, FTPClient ftpClient) throws Exception {
if (StringUtils.isBlank(ftpFile)) {
throw new RuntimeException("ftpFile为空");
}
if (StringUtils.isBlank(dstFile)) {
throw new RuntimeException("dstFile为空");
}
File file = new File(dstFile);
FileOutputStream fos = new FileOutputStream(file);
ftpClient.retrieveFile(ftpFile, fos);
fos.flush();
fos.close();
} /**
* 从文件服务器获取文件流
* @param ftpFile 文件服务器上的文件地址
* @return {@link InputStream}
* @throws IOException
*/
public static InputStream retrieveFileStream(String ftpFile, FTPClient ftpClient) throws IOException {
if (StringUtils.isBlank(ftpFile)) {
throw new RuntimeException("ftpFile为空");
}
return ftpClient.retrieveFileStream(ftpFile);
} public static void main(String[] args) {
try {
FileInputStream in=new FileInputStream(new File("E:\\qrcode.jpg"));
boolean flag = uploadFile("139.199.116.44", 21, "zdz69824436", "j67fBybHFS", "/autoCar/", "qrcode.jpg", in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

FTP上传心得的更多相关文章

  1. .net FTP上传文件

    FTP上传文件代码实现: private void UploadFileByWebClient() { WebClient webClient = new WebClient(); webClient ...

  2. 通过cmd完成FTP上传文件操作

    一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...

  3. FTP上传文件到服务器

    一.初始化上传控件. 1.我们这里用dropzone.js作为上传控件,下载地址http://www.dropzonejs.com/ 2.这里我们使用一个div元素作为dropzone载体. < ...

  4. 再看ftp上传文件

    前言 去年在项目中用到ftp上传文件,用FtpWebRequest和FtpWebResponse封装一个帮助类,这个在网上能找到很多,前台使用Uploadify控件,然后在服务器上搭建Ftp服务器,在 ...

  5. 【完整靠谱版】结合公司项目,仔细总结自己使用百度编辑器实现FTP上传的完整过程

    说在前面 工作中会遇到很多需要使用富文本编辑器的地方,比如我现在发布这篇文章离不开这个神器,而且现在网上编辑器太多了.记得之前,由于工作需要自己封装过一个编辑器的公共插件,是用ckeditor改版的, ...

  6. FTP上传文件提示550错误原因分析。

    今天测试FTP上传文件功能,同样的代码从自己的Demo移到正式的代码中,不能实现功能,并报 Stream rs = ftp.GetRequestStream()提示远程服务器返回错误: (550) 文 ...

  7. JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)

    package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...

  8. FTP上传-封装工具类

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...

  9. 记一次FTP上传文件总是超时的解决过程

    好久没写博,还是重拾记录一下吧. 背景:买了一个阿里云的云虚拟机用来搭建网站(起初不了解云虚拟主机和云服务器的区别,以为都是有SSH功能的,后来发现不是这样样子啊,云虚拟机就是FTP上传网页+MySQ ...

随机推荐

  1. Win7上安装scapy

    1.环境 操作环境:win7 python版本:python3.5 依赖模块:Npcap(推荐)或WinPcap 下载scapy 2.安装步骤 操作环境,python及依赖模块安装省略(一直点击下一步 ...

  2. IPV4/IPV6双协议栈配置案例

    拓扑: XRV1配置: =================================================================== hostname XRV1! ipv6 ...

  3. python的序列类

    1,我们常见的数据结构有哪些是序列类 序列类型的分类: ①  容器序列:list,tuple,deque(可以防止任意的类型的容器) ②  扁平序列:str,bytes,bytearray,array ...

  4. 【转载】Windows上那些值得推荐的良心软件-整理 easybcd 引导工具 easyuefi 引导工具

    您查询的关键词是:清理dism知乎 以下是该网页在北京时间 2019年03月17日 21:56:16 的快照: 如果打开速度慢,可以尝试快速版:如果想更新或删除快照,可以投诉快照. 百度和网页 htt ...

  5. F4 help for month

    INCLUDE rmcs0f0m. s_month FOR s001-spmon NO-EXTENSION NO INTERVALS OBLIGATORY. AT SELECTION-SCREEN O ...

  6. [leetcode]200. Number of Islands岛屿个数

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  7. related_name和related_query_name举例区别

    例1: class UserInfo(models.Model): nickname = models.CharField(max_length=32) username = models.CharF ...

  8. Log4Net web.config配置

     1 .[assembly: log4net.Config.XmlConfigurator(ConfigFile = "web.config", Watch = true)]  写 ...

  9. 关于requests库中文编码问题

    转自:代码分析Python requests库中文编码问题 Python reqeusts在作为代理爬虫节点抓取不同字符集网站时遇到的一些问题总结. 简单说就是中文乱码的问题.   如果单纯的抓取微博 ...

  10. Linux module 添加到bashrc 和临时ifort编译器 以及python2和3的配置

    第一步vim ~/.bashrc按键盘的i然后source /home/export/online1/bjpara/para/modules/scripts/cn-module.sh最后:x! bas ...