import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.*;
import javax.servlet.http.*;
public class CommServer extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK"; //编码类型
public void init() throws ServletException{ //初始化函数
}
public void yReceiverSend(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType(CONTENT_TYPE);
String ip = request.getRemoteHost(); //IP地址
String url = request.getRequestURI(); //url地址
InputStream inputStream = request.getInputStream(); //用二进制流接收数据
BufferedInputStream input = null; //输入流,用于接收请求的数据
byte[] buffer = new byte[1024]; //数据缓冲区
int count = 0; //每个缓冲区的实际数据长度
ByteArrayOutputStream streamXML = new ByteArrayOutputStream(); //请求数据存放对象
byte[] iXMLData = null; //请求数据存放到byte[]
//接收银行数据
try {
input = new BufferedInputStream(inputStream);
while ((count = input.read(buffer)) != -1){
streamXML.write(buffer, 0, count);
}
}catch (Exception e){
e.printStackTrace();
}finally{
if (input != null){
try {
input.close();
}catch (Exception f){
f.printStackTrace();
}
}
}
iXMLData = streamXML.toByteArray(); //得到一个byte数组,提供给平台
/
//调用相应平台程序的过程.......
// ......
// ......
/
//返回数据给银行
String returnStr = null; //平台程序返回的数据
try {
OutputStream out = response.getOutputStream();
response.setContentLength(returnStr.getBytes().length); //添加响应包长度
out.write(returnStr.getBytes());
out.flush();
out.close();
} catch (Exception e){
e.printStackTrace();
}
}
public void bSendReceiver() throws Exception{ //平台主动发起的http发送
try{
String url = "http://www.baidu.com"; //查表得到发送地址
HttpURLConnection httpConnection = (HttpURLConnection) new URL(url) //建立一个HttpURLConnection
.openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
httpConnection.setAllowUserInteraction(true);
httpConnection.connect();
//发送数据给保险公司
OutputStream outputStream = httpConnection.getOutputStream();
byte[] buffer = new byte[1024]; //平台生成的XML串
outputStream.write(buffer);
outputStream.flush();
outputStream.close();
//接收保险公司的数据数据
InputStream inputStream = httpConnection.getInputStream();
BufferedInputStream input = null; //输入流,用于接收请求的数据
buffer = new byte[1024]; //数据缓冲区
int count = 0; //每个缓冲区的实际数据长度
ByteArrayOutputStream streamXML = new ByteArrayOutputStream(); //请求数据存放对象
byte[] iXMLData = null; //请求数据存放到byte[]
try {
input = new BufferedInputStream(inputStream);
while ((count = input.read(buffer)) != -1){
streamXML.write(buffer, 0, count);
}
}catch (Exception e){
e.printStackTrace();
}finally{
if (input != null){
try {
input.close();
}catch (Exception f){
f.printStackTrace();
}
}
}
iXMLData = streamXML.toByteArray(); //得到一个byte数组,提供给平台
httpConnection.disconnect();
}catch (Exception e){
e.printStackTrace();
}
}
}