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集 ...
随机推荐
- APP测试流程
1 APP测试基本流程 1.1流程图 1.2测试周期 测试周期可按项目的开发周期来确定测试时间,一般测试时间为两三周(即15个工作日),根据项目情况以及版本质量可适当缩短或延长测试时间.正式测试前先向 ...
- 【POJ】2891 Strange Way to Express Integers
http://poj.org/problem?id=2891 题意:求最小的$x$使得$x \equiv r_i \pmod{ a_i }$. #include <cstdio> #inc ...
- 【ZOJ】1015 Fishing Net
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1015 题意:给出一个n个点的无向图,询问是否为弦图,弦图定义为对于图中任意 ...
- POI2012 (持续更新中)
Distance Well Vouchers Cloakroom A Horrible Poem Rendezvous Fibonacci Representation Squarks Salarie ...
- 数组的Clone方法
public void Test() { ,,}; var arr2 = arr1; var arr3 = (int[])arr1.Clone(); //浅拷贝 arr1[] = ; //arr2[0 ...
- BZOJ4154: [Ipsc2015]Generating Synergy
Description 给定一棵以1为根的有根树,初始所有节点颜色为1,每次将距离节点a不超过l的a的子节点染成c,或询问点a的颜色 Input 第一行一个数T,表示数据组数 接下来每组数据的第一 ...
- Java_Java中动态加载jar文件和class文件
转自:http://blog.csdn.net/mousebaby808/article/details/31788325 概述 诸如tomcat这样的服务器,在启动的时候会加载应用程序中lib目录下 ...
- Java_Java Compiler 应用实例
转自:http://hejiangtao.iteye.com/blog/1399122 一直在用JDK1.5, 一直搞不清楚JDK1.6有啥特性, 就翻了翻, 发现这个Compiler API(JSR ...
- (转)深入理解flash重绘
深入理解Flash Player重绘 Flash Player 会以SWF内容的帧频速度来刷新需要变化的内容,而这个刷新的过程,我们通常称为“重绘(redraw)”,相信即便是初级的菜鸟也知道,只要使 ...
- $.ajax用法与举例
下面是一段比较常用到的 $.ajax 方法: $.ajax({ type:'GET', url:'http://www.phpernote.com/jquery.php', data:{usernam ...