最近出了一个问题就是在本地上传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. Pandas.Series.dt.dayofweek相关命令

  2. CSS文本超出指定行数省略显示

    单行: overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 2行: font-size: 17px;overflow: hi ...

  3. mysql 报错You can't specify target table 'wms_cabinet_form' for update in FROM clause

    这个错误是说从t表select出来的无法又更新t表. 可以在select的时候先取个别名,弄个临时表即可.

  4. [leetcode]98. Validate Binary Search Tree验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  5. 46-web页面登入前和登入后控制

    可以将user存入session中,然后在前端根据能否取到user,来判断是否登入 <c:if test="${user == null }"> <li clas ...

  6. 使用U盘安装CentOS7

    为了在公司的旧笔记本电脑上安装centos供自己学习使用折腾了两天,终于在看了https://www.cnblogs.com/yeeo1/p/7306611.html这篇博文后装上了, 以下内容为转载 ...

  7. Python开发——数据类型【字符串格式化】

    字符串格式化之——% # 字符串格式化 msg = 'I am %s , My hobby is %s'%('yuan','play') print(msg) # I am yuan , My hob ...

  8. Netty学习路线总结

    序 之前开过品味性能系列.Mysql学习系列,颇为曲高和寡.都是讲理论,很少有手把手深入浅出的文章.不过确实我就这脾气,文雅点的说法叫做"伪雅",下里巴人叫做"装逼&qu ...

  9. foreach退出循环(新人请多多关照~)

    今天做一个关于人员信息修改的页面时,我用foreach获取数据库数据时发现,用if else判断输入的内容时,会一个一个的做对比,导致错误提醒时会弹出与数据库内容行数相同条的提醒,最后发现将数据直接命 ...

  10. 1.3eigen中数组类和系数的运算

    1.3数组类和系数的运算 与矩阵类只适用与线性代数运算相反,数组类提供通用的数组类,能不利用线性代数的知识来对系数进行操作,比如对每个系数加上一个常数,或者乘上两个数组的系数. 1.数组类型 跟矩阵类 ...