ftp读取文件


	public static boolean downloadFileBySun(String server, String userName,
			String userPassword) {
		String line;

		FtpClient ftpClient = new FtpClient();

		try {
			TelnetInputStream fileContent = null;
			TelnetInputStream fileDirectory = null;

			// 打开ftp服务器

			ftpClient.openServer(server);

			// 使用指定用户登录

			ftpClient.login(userName, userPassword);

			ftpClient.binary(); // 用二级制下载

			// ftpClient.cd("..");

			// fileContent = ftpClient
			// .get("\\EDIInputFiles\\Carrierbak\\118.txt");

			// fileContent =
			// ftpClient.get("\\EDIInputFiles\\Carrier\\CSOEDI\\bak\\");

			ftpClient.cd("\\EDIInputFiles\\Carrier\\CSOEDI\\bak\\");
			fileDirectory = ftpClient.list();

			// System.out.println(ftpClient.welcomeMsg);

			InputStreamReader ir = new InputStreamReader(fileDirectory, "GBK");

			BufferedReader br = new BufferedReader(ir);

			while ((line = br.readLine()) != null) {
				if (line.endsWith(".xls")) {
					String fileName = line.split(" ")[line.split(" ").length - 1];
					fileContent = ftpClient.get(fileName);
					InputStreamReader cir = new InputStreamReader(fileContent,
							"GBK");
					BufferedReader cbr = new BufferedReader(cir);

					String line1;

					while ((line1 = cbr.readLine()) != null) {
						System.out.println(line1);
					}

				}
			}
			br.close();
			ir.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			try {

				// 关闭ftp连接

				ftpClient.closeServer();

			} catch (Exception e) {

				e.printStackTrace();

			}

		}

		return true;

	}

	public static boolean uploadFileBySun(StringBuffer fileContent,
			String server, String userName, String userPassword, String path,
			String fileName) {

		FtpClient ftpClient = new FtpClient();

		try {

			// 打开ftp服务器

			ftpClient.openServer(server);

			// 使用指定用户登录

			ftpClient.login(userName, userPassword);

			// 转到指定路径

			ftpClient.cd(path);

			TelnetOutputStream os = null;

			// 新建一个文件

			// os = ftpClient.put(new String(fileName.getBytes("GBK"),
			// "iso-8859-1"));

			os = ftpClient.put(fileName);

			OutputStreamWriter osw = new OutputStreamWriter(os);

			BufferedWriter bw = new BufferedWriter(osw);

			bw.write(fileContent.toString());

			bw.flush();

			bw.close();

		} catch (Exception e) {

			System.out.println(e.getMessage());

			return false;

		} finally {

			try {

				// 关闭ftp连接

				ftpClient.closeServer();

			} catch (Exception e) {

				e.printStackTrace();

			}

		}

		return true;

	}
	

你可能感兴趣的:(读取文件)