FTP上传
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上传的更多相关文章
- .net FTP上传文件
FTP上传文件代码实现: private void UploadFileByWebClient() { WebClient webClient = new WebClient(); webClient ...
- 通过cmd完成FTP上传文件操作
一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...
- FTP上传文件到服务器
一.初始化上传控件. 1.我们这里用dropzone.js作为上传控件,下载地址http://www.dropzonejs.com/ 2.这里我们使用一个div元素作为dropzone载体. < ...
- 再看ftp上传文件
前言 去年在项目中用到ftp上传文件,用FtpWebRequest和FtpWebResponse封装一个帮助类,这个在网上能找到很多,前台使用Uploadify控件,然后在服务器上搭建Ftp服务器,在 ...
- 【完整靠谱版】结合公司项目,仔细总结自己使用百度编辑器实现FTP上传的完整过程
说在前面 工作中会遇到很多需要使用富文本编辑器的地方,比如我现在发布这篇文章离不开这个神器,而且现在网上编辑器太多了.记得之前,由于工作需要自己封装过一个编辑器的公共插件,是用ckeditor改版的, ...
- FTP上传文件提示550错误原因分析。
今天测试FTP上传文件功能,同样的代码从自己的Demo移到正式的代码中,不能实现功能,并报 Stream rs = ftp.GetRequestStream()提示远程服务器返回错误: (550) 文 ...
- JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)
package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...
- FTP上传-封装工具类
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
- 记一次FTP上传文件总是超时的解决过程
好久没写博,还是重拾记录一下吧. 背景:买了一个阿里云的云虚拟机用来搭建网站(起初不了解云虚拟主机和云服务器的区别,以为都是有SSH功能的,后来发现不是这样样子啊,云虚拟机就是FTP上传网页+MySQ ...
- FlashFXP(强大的FXP/ftp上传工具)V5.0.0.3722简体中文特别版
flashfxp是功能强大的fxp/ftp软件,融合了一些其他优秀ftp软件的优点,如像cuteftp一样可以比较文件夹, FlashFXP是一款功能强大的FXP/ftp上传工具, FlashFXP集 ...
随机推荐
- Android -- TextView、button方法详解(1)
1.TextView常规方法 TextView myTextView=null; //声明变量 myTextView=(TextView)findViewById(R.id.myTextView); ...
- 显式激活数据库( ACTIVATE DATABASE)
某天值班员联系我说,我负责的一套报送系统没有按时生成报文,因为此报警提前量比较大,加上系统经常发生未按时生成报文的事件,也就是没在意,然后不急不慢的到公司,打开系统页面,发现其中一个存储过程跑了将近8 ...
- 僵尸进程的产生和避免,如何kill杀掉linux系统中的僵尸defunct进程
在 Unix系统管理中,当用ps命令观察进程的执行状态时,经常看到某些进程的状态栏为defunct,这就是所谓的"僵尸"进程."僵尸"进程是一个早已 死亡的进程 ...
- usaco silver刷水~其实是回顾一下,补题解
[BZOJ1606][Usaco2008 Dec]Hay For Sale 裸01背包 ;i<=n;i++) for(int j=m;j>=a[i];j--) f[j]=max(f[j], ...
- Maven_dependencies 和 dependencyManagement 的区别
今天我在配置 sellercenter 的接口测试环境的时候,发现一些依赖的写法不太一致: 比如有的依赖的<scope>是写在子项目中的 <dependencies> 下的&l ...
- hbases索引技术:Lily HBase Indexer介绍
Lily HBase Indexer 为hbase提供快速查询,他允许不写代码,快速容易的把hbase行索引到solr.Lily HBase Indexer drives HBase indexing ...
- Json数据格式事例查看
最简单的一种: [{"ProvinceID":1,"ProvinceName":"北京市","DateCreated": ...
- iOS中JSONModel的使用
iOS中JSONModel的使用 流弊的JSON数据模型框架 https://github.com/jsonmodel/jsonmodel 版本 1.3.0 如果你喜欢JSONModel,并且使用 ...
- iOS开发中几个重要的方法
iOS开发中几个重要的方法: 加载类到内存,程序刚启动的时候调用,调用在main函数之前 1.+(void)load{ } 初始化类,类第一次使用的时候调用一次 2.+(void)initialize ...
- inconfont 字体库应用
先去注册个号码,好像只可以用新浪微博登录哈,搞一个微博去. 第一就是点上面图标库,选择官方和所有都行. 恩接着点一个图标,他就自己跑到 第二个按钮哪里去了,在点第二个按钮,会出来一个创建项目,随便创建 ...