最近出了一个问题就是在本地上传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. Linux上web服务器搭建

    安装php依赖包: yum -y install gcc gcc++ libxml2 libxml2-devel yum install gcc make gd-devel libjpeg-devel ...

  2. Python+Selenium学习--自动生成HTML测试报告

    前言 在脚本运行完成之后,除了在log.txt 文件看到运行日志外,我们更希望能生一张漂亮的测试报告来展示用例执行的结果.        HTMLTestRunner 是Python 标准库的unit ...

  3. java 导mysql数据为表格给浏览器接收

    jar 包准备 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</a ...

  4. 281. Zigzag Iterator z字型遍历

    [抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...

  5. [leetcode]91. Decode Ways解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  6. Linux驱动之异步OR同步,阻塞OR非阻塞概念介绍

    链接:https://www.zhihu.com/question/19732473/answer/20851256 1.同步与异步同步和异步关注的是消息通信机制 (synchronous commu ...

  7. day3-selenium的使用及

    如果是python2的情况下需要设置: # _*_ coding:utf-8 _*_,这样的编码来保证输入中文在运行时不会报错,另外在中文的前边加上u保证编译时不会报错 from selenium i ...

  8. AutoCAD开发5--批量修改dwg文件

    Dim files, path, filename path = ThisDrawing.Utility.GetString(True, "输入dwg文件所在路径:") 'dwg文 ...

  9. linux安装mysql和httpd

    1.安装前检查是否已经安装[root@localhost1 ~]# rpm -qa |grep mysql 2.安装wget包:[root@localhost1 ~]# yum -y install ...

  10. 关于git 命令的一些事

    克隆代码命令 http://www.yiibai.com/git/git_clone.html 关键:得实现新建本地仓库文件夹 ==> git clone 远程网址 git 上传主要代码:htt ...