package cn.zto.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException; import org.apache.log4j.Logger; import cn.zto.log4j.Log4jConfigurator;
import cn.zto.pusher.HeNanPusher;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream; //import sun.net.ftp.FtpClient;
import org.apache.commons.net.ftp.*; @SuppressWarnings({ "restriction", "unused" })
public class FtpUtil {
private Logger log = Log4jConfigurator.getLogger(FtpUtil.class);
private static String encoding = System.getProperty("file.encoding");
/**
* 本地文件名
*/
private String localfilename;
/**
* 远程文件名
*/
private String remotefilename;
/**
* FTP客户端
*/
private FTPClient ftpClient; /**
* 服务器连接
*
* @param ip
* 服务器IP
* @param port
* 服务器端口
* @param user
* 用户名
* @param password
* 密码
* @param path
* 服务器路径
*/
public boolean connect(String hostname, int port, String username,
String password) {
ftpClient = new FTPClient();
try {
ftpClient.connect(hostname, port);
ftpClient.setControlEncoding("UTF-8");
ftpClient.setConnectTimeout(60000);
ftpClient.setSoTimeout(60000);
ftpClient.setDefaultTimeout(60000);
ftpClient.setDataTimeout(60000); if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
if (ftpClient.login(username, password)) {
ftpClient.enterLocalActiveMode();//设置主动模式 登录之后设置
this.log.error("connect success" + hostname);
return true;
} else {
this.log.error("connect error:" + hostname);
return false;
}
} else {
this.log.error("connect error:" + hostname);
return false;
}
} catch (Exception e) {
e.printStackTrace();
this.log.error("connect error" + hostname);
return false;
} } // 关闭连接
public void disconnect() throws IOException {
if (ftpClient.isConnected()) {
ftpClient.disconnect();
this.log.error("断开连接");
}
} /**
* 上传文件
*/
public boolean upload(String remotefilename, String xml) { this.remotefilename = remotefilename;
// 判断上传的文件在FTP服务器下是否已经存在
/*FTPFile[] files;
try {
files = ftpClient.listFiles(remotefilename);
if (files.length > 0) {
this.log.error("File already exists");
return true;
}
} catch (IOException e) {
this.log.error("find file error"+e);
return false;
}*/
InputStream input = null;
try {
input = new ByteArrayInputStream(xml.getBytes("utf-8"));
ftpClient.changeWorkingDirectory(new String(remotefilename
.getBytes(encoding), "iso-8859-1"));
if (ftpClient.storeFile(remotefilename, input)) {
this.log.error("upload success:"+remotefilename);
return true;
} else {
this.log.error("upload failed"+remotefilename);
return false;
}
} catch (IOException ex) {
this.log.error("not upload" + ex.getMessage());
return false;
}finally{
if(input!=null){
try {
input.close();
this.log.error("关闭输入流");
} catch (IOException 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 ...

  10. FlashFXP(强大的FXP/ftp上传工具)V5.0.0.3722简体中文特别版

    flashfxp是功能强大的fxp/ftp软件,融合了一些其他优秀ftp软件的优点,如像cuteftp一样可以比较文件夹, FlashFXP是一款功能强大的FXP/ftp上传工具, FlashFXP集 ...

随机推荐

  1. VB远程访问MYSQL代码图解

    首先需要安装MySQL Connector/ODBC 地址:http://dev.mysql.com/downloads/connector/odbc/ 根据自己的系统(运行环境: Win7/XP/W ...

  2. windows与linux之间传输文件

    1.使用SSH Secure Shell Client 百度网盘 下载地址 http://pan.baidu.com/s/1kTmp00J 2.使用pscp 百度网盘地址:http://pan.bai ...

  3. $.ajax等相关用法

    下面是jquery一些方法的相关用法: $.ajax: $.ajax({ type: "GET", url: "url", data: {username:$( ...

  4. linux查看memcached状态

    一.使用memcache top脚本 memcache-top 是一个用 Perl 编写的命令行小工具,用来实时的获取 memcached 缓存服务器的详细信息,例如缓存命中率等.到官网下载脚本,放到 ...

  5. java-两个大数相加

    题目要求:用字符串模拟两个大数相加. 一.使用BigInteger类.BigDecimal类 public static void main(String[] args) { String a=&qu ...

  6. 20个有用的jq弹窗

    链接地址:http://www.ffpic.com/zixun/20-useful-jquery-modal-plugins.html 推荐:jQuery.bPopup.js

  7. 最新php一句话木马

    <?php $uf=”snc3″; $ka=”IEBldmFbsK”; $pjt=”CRfUE9TVF”; $vbl = str_replace(“ti”,””,”tistittirti_rti ...

  8. lua函数

      一.函数 在lua中函数的调用方式和C语言基本相同. 如print(“hello world”), z=add(x+y).唯一的差别是,如果函数只有一个参数,并且该参数是字符串或者table构造器 ...

  9. 【HDU2196 Computer】经典树形dp

    http://acm.hdu.edu.cn/showproblem.php?pid=2196 题意:有n台电脑相连,让你求每台电脑与离它最远的那台电脑的距离. 思路:两遍搜索即可,第一遍从上到下,第二 ...

  10. asp.net mvc Post上传文件大小限制

    最近发现在项目中使用jQuery.form插件上传比较大的文件时,上传不了,于是改了下web.config的上传文件最大限制. <configuration> <system.web ...