netclientimpl.java

来自「一个信息采集系统...对刚接触JSP和JAVA的人来说还有用」· Java 代码 · 共 84 行

JAVA
84
字号
package com.briup.inf.impl;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Collection;
import java.util.Properties;

import com.briup.exception.BackupException;
import com.briup.exception.NetClientException;
import com.briup.inf.*;
import com.briup.inf.NetClient;

/**
 * class NetClientImpl
 * 
 * @author Jimmy Zhou
 * @Date 2008-2-4 下午11:35:26
 */
public class NetClientImpl implements NetClient {

	private final String host;

	private final int port;

	public NetClientImpl(Properties pro) {
		host = pro.getProperty("host");
		port = Integer.parseInt(pro.getProperty("port"));
	}

	public void send(Collection bIDRS) throws NetClientException {
		Socket s = null;
		ObjectOutputStream oos = null;
		try {
			s = new Socket(host, port);
			// 获取输出流s.getOutputStream();,然后包装成对象
			oos = new ObjectOutputStream(s.getOutputStream());
			oos.writeObject(bIDRS);
			oos.flush();
		} catch (Exception e) {
			e.printStackTrace();
			throw new NetClientException(e);
		} finally {
			try {
				if (oos != null) {
					oos.close();
				}
				if (s != null) {
					s.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/*
	 * 1、获取config实例 2、获取Gather,Log,Backup,NetClient 3、调用gather采集数据
	 * 4、调用backup备份数据 5、调用netClient发送数据
	 */
	public static void main(String[] agrs) {
		Collection c = null;
		Properties pro = new Properties();
		pro.setProperty("config_file_path", "src/com/briup/config.xml");
		com.briup.inf.Config config = ConfigImpl.newInstance(pro);
		Backup bu = null;

		try {
			com.briup.inf.Gather gather = config.getGather();
			bu = config.getBackup();
			NetClient nc = config.getNetClient();
			c = gather.doGather();
			nc.send(c);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				bu.load();
			} catch (BackupException e1) {
				e1.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?