通过FTPClient向ftp服务器上传文件

FTPClient 要实现上传和下载,需要jakarta-oro-2.0.8.jar,commons-net-1.4.1.jar这两个jar包。

public boolean uploadFile(String filename,InputStream input){
		 boolean success = false;
		FTPClient client = new FTPClient();
		try {
			client.connect("ip地址", 端口号);
			client.login("用户名", "密码");
			int reply = client.getReplyCode();
			if(!FTPReply.isPositiveCompletion(reply)){
				client.disconnect();
				return success;
			}
			client.enterLocalPassiveMode();
			//client.changeWorkingDirectory(pathname);
			FTPFile[] files = client.listFiles();
			System.out.println(files.length);
			client.storeFile(filename, input);
			input.close();
			client.logout();
			success = true;
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if (client.isConnected()) {
				try {
					client.disconnect();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return success;
	}  

    我们在client.listFiles()之前加上这句client.enterLocalPassiveMode();防止执行listFiles()的时候程序出现“卡死”的现象。

猜你喜欢

转载自zhen217.iteye.com/blog/2202553
今日推荐