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();
				}
			}
		}

	}

}

  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。