JAVA--JDK 1.7连接FTP服务器_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > JAVA--JDK 1.7连接FTP服务器

JAVA--JDK 1.7连接FTP服务器

 2014/11/8 22:08:38  chendaiming  程序员俱乐部  我要评论(0)
  • 摘要:FTPUtil.javapackagecom.ftp;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.InetSocketAddress;importjava.net.SocketAddress
  • 标签:jdk Java 连接 服务器 服务

FTPUtil.java

class="java" name="code">package com.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpProtocolException;

public class FTPUtil {
	/**
	 * 连接ftp服务器 JDK 1.7
	 * @param url
	 * @param port
	 * @param username
	 * @param password
	 * @return FtpClient
	 * @throws FtpProtocolException
	 * @throws IOException
	 */
	public static FtpClient connectFTP(String url,int port,String username,String password) {
		//创建ftp
		FtpClient ftp = null;
		try {
			//创建地址
			SocketAddress addr = new InetSocketAddress(url,port);
			//连接
			ftp = FtpClient.create();
			ftp.connect(addr);
			//登陆
			ftp.login(username,password.toCharArray());
			ftp.setBinaryType();
			System.out.println(ftp.getWelcomeMsg());
		} catch (FtpProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ftp;
	}
	
	/**切换目录
	 * @param ftp
	 * @param path
	 */
	public static void changeDirectory(FtpClient ftp,String path) {
		try {
			ftp.changeDirectory(path);
			
			System.out.println(ftp.getWorkingDirectory());
		} catch (FtpProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 关闭ftp
	 * @param ftp
	 */
	public static void disconnectFTP(FtpClient ftp){
		try {
			ftp.close();
			System.out.println("disconnect success!!");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 上传文件
	 * @param localFile
	 * @param ftpFile
	 * @param ftp
	 */
	public static void upload(String localFile,String ftpFile,FtpClient ftp){
		OutputStream os = null;
		FileInputStream fis = null;
		try {
			//将ftp文件加入输出流中。输出到ftp上
			os = ftp.putFileStream(ftpFile);
			File file = new File(localFile);
			//创建一个缓冲区
			fis = new FileInputStream(file);
			byte[] bytes = new byte[1024];
			int c;
			while((c = fis.read(bytes))!=-1){
				os.write(bytes,0,c);
			}
			System.out.println("upload success!!");
		} catch (FtpProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(os!=null)
					os.close();
				if(fis!=null)
					fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}
	
	/**
	 * 文件下载
	 * @param localFile
	 * @param ftpFile
	 * @param ftp
	 */
	public static void download(String localFile,String ftpFile,FtpClient ftp){
		InputStream is = null;
		FileOutputStream fos = null;
			try {
				//获取ftp上的文件
				is = ftp.getFileStream(ftpFile);
				File file = new File(localFile);
				byte[] bytes = new byte[1024];
				int i;
				fos = new FileOutputStream(file);
				while((i = is.read(bytes))!=-1){
					fos.write(bytes, 0, i);
				}
				System.out.println("download success!!");
				
			} catch (FtpProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					if(fos!=null)
						fos.close();
					if(is!=null){
						is.close();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
	}
	
}

?fileOpt.java

package file.opt;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.text.SimpleDateFormat;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpProtocolException;

/**
 * Java自带的API对FTP的操作
 * @Title:Ftp.java
 * @author: wdd
 **/
public class fileOpt {
	private String localfilename;
	private String remotefilename;
	private FtpClient ftpClient;

	public void connectServer(String ip, int port, String user,
			String password, String path) {
		try { 
			ftpClient = FtpClient.create();
			try {
				SocketAddress addr = new InetSocketAddress(ip, port);
				ftpClient.connect(addr);
				ftpClient.login(user, password.toCharArray());
				System.out.println("login success!");
				if (path.length() != 0) { // 把远程系统上的目录切换到参数path所指定的目录
					ftpClient.changeDirectory(path);
				}
			} catch (FtpProtocolException e) {
				e.printStackTrace();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}

	/** * 关闭连接 * @author wdd * @date 2014-9-1 */
	public void closeConnect() {
		try {
			ftpClient.close();
			System.out.println("disconnect success");
		} catch (IOException ex) {
			System.out.println("not disconnect");
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}

	/**
	 * * 上传文件 * @param localFile 本地文件 * @param remoteFile 远程文件 * @author wdd * @date
	 * 2014-9-1
	 */
	public void upload(String localFile, String remoteFile) {
		this.localfilename = localFile;
		this.remotefilename = remoteFile;
		OutputStream os = null;
		FileInputStream is = null;
		try { // 将远程文件加入输出流中
			try {
				os = ftpClient.putFileStream(this.remotefilename);
			} catch (FtpProtocolException e) { // TODO Auto-generated catch
												// block
				e.printStackTrace();
			} // 获取本地文件的输入流
			File file_in = new File(this.localfilename);
			is = new FileInputStream(file_in); // 创建一个缓冲区
			byte[] bytes = new byte[1024];
			int c;
			while ((c = is.read(bytes)) != -1) {
				os.write(bytes, 0, c);
			}
			System.out.println("upload success");
		} catch (IOException ex) {
			System.out.println("not upload");
			ex.printStackTrace();
			throw new RuntimeException(ex);
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (os != null) {
						os.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * * 下载文件 * @param remoteFile 远程文件路径(服务器端) * @param localFile 本地文件路径(客户端) * @author
	 * wdd * @date 2014-9-1
	 */
	public void download(String remoteFile, String localFile) {
		InputStream is = null;
		FileOutputStream os = null;
		try {
			// 获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。
			try {
				is = ftpClient.getFileStream(remoteFile);
			} catch (FtpProtocolException e) {
				e.printStackTrace();
			}
			File file_in = new File(localFile);
			os = new FileOutputStream(file_in);
			byte[] bytes = new byte[1024];
			int c;
			while ((c = is.read(bytes)) != -1) {
				os.write(bytes, 0, c);
			}
			System.out.println("download success");
		} catch (IOException ex) {
			System.out.println("not download");
			ex.printStackTrace();
			throw new RuntimeException(ex);
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (os != null) {
						os.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}

?Test.java

package test;

import java.net.InetAddress;
import java.net.UnknownHostException;

import sun.net.ftp.FtpClient;

import com.ftp.FTPUtil;

public class Test {
	public static void main(String[] args) {
		try {
			InetAddress addr =  InetAddress.getLocalHost();
			System.out.println(addr.getHostAddress());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String ip = "10.10.10.10";
		int port = 21;
		String username = "root";
		String password = "root";
		String path = "/home";
		//连接ftp
		FtpClient ftp = FTPUtil.connectFTP(ip, port, username, password);
		System.out.println(ftp.getWelcomeMsg());
		//切换目录
		FTPUtil.changeDirectory(ftp, path);
		System.out.println("-----上传----");
		FTPUtil.upload("D:\\aaa.txt", "/home/aaa.txt", ftp);
		System.out.println("-----下载----");
		FTPUtil.download("D:\\aaa.txt", "/home/aaa.txt", ftp);
	}

}

?

上一篇: Java抽象画--秒变绘图大师 下一篇: 没有下一篇了!
发表评论
用户名: 匿名