FTPUtil

public class FTPUtil {
	private static String encoding = System.getProperty("file.encoding");
	
	private FTPClient ftpClient;
	
	public FTPUtil(String url,int port,String username,String password) {
		ftpClient = new FTPClient();
		try {
			ftpClient.connect(url,port);
			ftpClient.login(username, password);
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			ftpClient.setControlEncoding(encoding);
			ftpClient.enterLocalPassiveMode();
			ftpClient.setBufferSize(1024);
			FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);//解决中文乱码   
			conf.setServerLanguageCode("zh"); 
			ftpClient.configure(conf);
		} catch (SocketException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP连接失败!", e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP客户端出错!", e);
		}
	}
	public FTPClient connect(String url,int port,String username,String password) {
		ftpClient = new FTPClient();
		try {
			ftpClient.connect(url,port);
			ftpClient.login(username, password);
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			ftpClient.setControlEncoding(encoding);
			ftpClient.enterLocalPassiveMode();
			ftpClient.setBufferSize(1024);
			FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);   
			conf.setServerLanguageCode("zh"); 
			ftpClient.configure(conf);
		} catch (SocketException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP连接失败!", e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP客户端出错!", e);
		}
        return ftpClient;
	}
	
	public void uploadFile(String src,String des) {
		File srcFile = new File(src);
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(srcFile);
			ftpClient.storeFile(des, fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException("文件未找到!", e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP客户端出错!", e);
		} finally {
            IOUtils.closeQuietly(fis);
        }
	}
	
	public void downLoadFile(String remote,String local) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(local);
			FTPFile[] files = ftpClient.listFiles();
			System.out.println(files[0].getName());
			ftpClient.retrieveFile(remote, fos);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException("文件未找到!", e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP客户端出错!", e);
		} finally {
            IOUtils.closeQuietly(fos);
        }
	}
	
	public boolean isDirExists(String name) {
		boolean exist = false;
		if(StringUtils.isBlank(name)) {
			return exist;
		}
		try {
			FTPFile[] files = ftpClient.listDirectories();
			for(FTPFile file : files) {
				if(name.equals(file.getName())) {
					exist = true;
					return exist;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP客户端出错!", e);
		}
		return exist;
	}
	public void closeConnect() {
		try {
			ftpClient.disconnect();
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("关闭FTP连接发生异常!", e);
		}
	}
	
	public void cd(String dir) {
		try {
			ftpClient.changeWorkingDirectory(dir);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP发生异常!", e);
		}
	}
	
	public void mkdir(String dir) {
		try {
			ftpClient.makeDirectory(dir);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP发生异常!", e);
		}
	}
}

### 回答1: 当使用ftputil进行FTP操作时,可能会遇到“null”错误。这个错误通常是由于以下原因之一引起的: 1. FTP服务器连接问题:请确保你的FTP服务器地址、端口、用户名和密码正确。如果其中一个参数有误,可能会导致连接失败并返回“null”错误。请检查这些参数,并确保它们与你的FTP服务器设置匹配。 2. 文件/路径错误:如果你尝试访问一个不存在的文件或路径,也会导致“null”错误。请确保你要访问的文件/路径存在,并且你有足够的权限进行访问。 3. 网络问题:有时网络连接不稳定或中断可能导致FTP操作失败,并返回“null”错误。如果你的网络连接不稳定,请尝试使用稳定的网络连接再次运行操作。 4. 代码错误:在编写ftputil代码时,可能会有语法错误或逻辑错误导致“null”错误。请仔细检查你的代码,并确保它没有任何问题。如果发现问题,请进行适当的更正。 总结来说,ftputil报“null”错误可能是由于FTP服务器连接问题、文件/路径错误、网络问题或代码错误引起的。通过仔细检查和排除这些可能性,你应该能够找到并解决问题。 ### 回答2: 根据问题描述,在使用ftputil时遇到了"null"错误。"null"错误通常表示某个变量或对象的值为null,因此需要进一步检查具体的代码和错误提示以确定问题所在。 1. 首先,确保ftputil库已经正确安装,并且在代码中正确导入了该库。 2. 检查是否正确设置了FTP服务器的连接参数,包括主机地址、端口号、用户名和密码等。确保这些参数没有设置为null,而是正确设置为相应的值。 3. 如果使用的是Python 3.x版本,请检查代码中是否采用了正确的语法和函数调用。在Python 3.x中,某些函数的用法可能与之前的版本不同。 4. 检查代码中可能出现的其他错误。例如,在连接FTP服务器之前,可能需要先初始化ftputil库的某些配置;或者在上传或下载文件时,需要先检查本地文件是否存在于指定路径中。 5. 最后,查看具体的错误提示信息,尤其是报错的行数和具体的错误描述。这些信息能够提供更多的线索,帮助我们确定问题所在。 综上所述,要解决ftputil报"null"错误,需要检查代码中的配置参数、语法、函数调用以及其他潜在的错误,并仔细查看错误提示信息,找出具体的问题所在。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值