首页>代码>apache hessian原理学习和自己实现rpc远程调用实例代码>/hessian-study/modules/hessian-study-server/src/main/java/com/jzx/httprpc/HttpProxy.java
                
                package com.jzx.httprpc;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLConnection;
import com.jzx.hessian.vo.UserVo;
import com.jzx.httprpc.utils.ObjectUtils;
public class HttpProxy implements InvocationHandler {
	String url = "";
	HttpProxyFactory httpProxyFactory;
	Class<?> api;
	public HttpProxy(String url, HttpProxyFactory httpProxyFactory, Class<?> api) {
		this.url = url;
		this.httpProxyFactory = httpProxyFactory;
		this.api = api;
	}
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		HttpTransportation transportation = new HttpTransportation();
		transportation.setClazz(api);
		transportation.setMethod(method.getName());
		transportation.setParams(args);
		byte[] bytes = ObjectUtils.objectToByte(transportation);
		Object result = sendPost(url, bytes);
		return result;
	}
	// 发送POST请求
	public static Object sendPost(String url, byte[] param) {
		PrintWriter out = null;
		BufferedReader in = null;
		Object result = null;
		try {
			URL realUrl = new URL(url);
			// 打开和URL之间的连接
			URLConnection conn = realUrl.openConnection();
			// 设置通用的请求属性
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输出流
			OutputStream outputStream = conn.getOutputStream();
			outputStream.write(param);
			outputStream.close();
			// 定义BufferedReader输入流来读取URL的响应
			// in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			byte[] bytes = toByteArray(conn.getInputStream());
			Object object = ObjectUtils.byteToObject(bytes);
			if (object instanceof String) {
				byte[] dest = new byte[bytes.length - 7];
				System.arraycopy(bytes, 7, dest, 0, dest.length);
				result = new String(dest, "UTF-8");
//				result = new String(bytes, "UTF-8");
			}
			if (object instanceof UserVo) {
				result = object;
			}
		} catch (Exception e) {
			System.out.println("发送 POST 请求出现异常!" + e);
			e.printStackTrace();
		}
		// 使用finally块来关闭输出流、输入流
		finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}
	public static byte[] subBytes(byte[] src, int begin, int count) {
		byte[] bs = new byte[count];
		for (int i = begin; i < begin + count; i++)
			bs[i - begin] = src[i];
		return bs;
	}
	// 下面是IOUtils中摘录出与toByteArray相关的方法
	// org.apache.commons.io.IOUtils.toByteArray
	public static byte[] toByteArray(InputStream input) throws IOException {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		copy(input, output);
		return output.toByteArray();
	}
	public static int copy(InputStream input, OutputStream output) throws IOException {
		long count = copyLarge(input, output);
		if (count > 2147483647L) {
			return -1;
		}
		return (int) count;
	}
	public static long copyLarge(InputStream input, OutputStream output) throws IOException {
		byte[] buffer = new byte[4096];
		long count = 0L;
		int n = 0;
		while (-1 != (n = input.read(buffer))) {
			output.write(buffer, 0, n);
			count += n;
		}
		return count;
	}
}
 最近下载更多
最近下载更多
                
 
                 
     
                 
                 最近浏览
最近浏览