FTP读取远程文件&解决使用FTPClient类时线程挂起的问题

一、首先介绍FTP的基本知识(会的直接跳过)

1.什么是FTP

  FTPFile Transfer Protocol文件传输协议)的英文简称,而中文简称为文传协议。用于Internet上的控制文件的双向传输。同时,它也是一个应用程序Application说白了,就是文件传输的规则,方法。与大多数Internet服务一样,FTP也是一个客户机/服务器系统。用户通过一个支持FTP协议的客户机程序,连接到在远程主机上的FTP服务器程序。用户通过客户机程序向服务器程序发出命令,服务器程序执行用户所发出的命令,并将执行的结果返回到客户机。

2.FTP适合哪种操作系统

  常见的操作系统均可以,而且可以说实现在不同操作系统间进行文件的传输

3.一个简单的FTP使用

①安装FTP服务器

参照:http://blog.csdn.net/exlsunshine/article/details/29181465

SSL那个可以选无SSL证书

②通过客户端访问

前提:服务器电脑关闭了防火墙设置

方法:ftp://要访问的IP

二、使用java代码访问远程服务器并读取文件

1.代码如下

使用的是FTPClient类,用到的jar包有commons-net-3.3.jarjakarta-oro-2.0.8.jarjavacsv.jarjdk1.8

资源下载

http://download.csdn.net/detail/kunfd/9926398

import java.io.*;
import java.net.SocketException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class FTP {
	// 设置服务主机的地址,密码等基本信息

	private String ip = "远程主机ip";
	private String userName = "远程主机用户名";
	private String userPwd = "远程主机用户密码";
	private int port = 21;

	private FTPClient ftpClient = new FTPClient();
	public FTP(){
		try {
			connectionServer(ftpClient,ip,port,userName,userPwd);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//连接远程服务器
	public void connectionServer(FTPClient ftpClient,String ip,int port,String userName,String userPwd) throws IOException {

		ftpClient.connect(ip,port);//连接主机,方法继承于SocketClient
		ftpClient.login(userName,userPwd);//登录,成功的话返回ture
		ftpClient.enterLocalPassiveMode();//设置为主动动模式
	}
	//文件上传功能
	public boolean uploadFile(InputStream iStream, String newName)   throws IOException {
		boolean flag = false;
		try {
			ftpClient.setDataTimeout(30000);//设置超时
			ftpClient.setSoTimeout(30000);
			ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);//设置传输模式

			flag = ftpClient.storeFile(newName, iStream);//根据输入流传输文件
		} catch (Exception e) {
			flag = false;
			return flag;
		} finally {
			if (iStream != null) {
				iStream.close();
			}
		}
		return flag;
	}
	//读取服务器上指定文件并输出到控制台
	public void readFile(String remote,String pathname) throws IOException {
		ftpClient.setDataTimeout(30000);
		ftpClient.setSoTimeout(30000);
		ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
		ftpClient.changeWorkingDirectory(pathname);//跳转到指定位置

		InputStream in = ftpClient.retrieveFileStream(remote);//打开指定位置的文件,返回一个输入流,给外界读取
		BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
		String line;
		while ((line = reader.readLine()) != null) {
			System.out.println(line);
		}
	}
	//删除指定文件
	public void deleteFile(String fileName) {
		try {
			ftpClient.deleteFile(fileName);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) throws Exception {
		FTP ftp = new FTP();
		//boolean b= ftp.uploadFile(new BufferedInputStream(new FileInputStream("G:\\test.txt")),"test.txt");
		ftp.readFile("test.txt","test");
		//ftp.deleteFile("test.txt");
	}
}

5.FTP的使用注意事项

①防火墙注意事项

②当使用FTPClient中的方法比如上传setFileType,读取retrieveFileStream的时候,注意要将FTP设置为被动模式

enterLocalPassiveMode(要不然会导致线程挂起)

6.参考内容

http://blog.csdn.net/huanggang028/article/details/41446707

猜你喜欢

转载自blog.csdn.net/kunfd/article/details/76991897
今日推荐