2020-11-05 系统之间的数据交互

我常用的系统之间的数据交互方式有:

1.HTTP  这个现在比较多,一般数据是JSON/XML格式

2.WEBSERVICE 传统互联网金融银行保险用的比较多,电商好像基本不用

3.socket  用的比较少了 老旧项目用的多

4.SFTP/FTP 现在因为安全问题一般都是使用SFTP

5.系统之间共用数据库可能会数据库直接授权。

6.有些服务集群可能会有共享磁盘,大家都有权限去读写。

7.MQ

8.集群有时候中间会过一道NGINX APACHE 之类的

下面放一个http例子:

package com.example.test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class TestHttps {

	public static void main(String[] args) throws UnsupportedEncodingException  {
 
  	String aString = "7898989";
  	
  	String urlString = "http:******************* ";
   	executeHttpPostMethod(urlString,aString,"UTF-8");
  }
	 public static String executeHttpPostMethod(String requestUrl,String request,String charsetName) {
			String result = "";
			HttpsURLConnection httpURLConnection = null;
			ByteArrayOutputStream outputStream = null;
			InputStream inputStream = null;
			try {
				URL url = new URL(requestUrl);
				httpURLConnection = (HttpsURLConnection) url.openConnection();
				httpURLConnection.setDoOutput(true);
				httpURLConnection.setDoInput(true);
				httpURLConnection.setRequestProperty("Content-Type","text/plain");
				httpURLConnection.setRequestMethod("POST");
				httpURLConnection.setInstanceFollowRedirects(true);
				httpURLConnection.setConnectTimeout(60 * 1000);
				httpURLConnection.connect();
				httpURLConnection.getOutputStream().write(request.getBytes(charsetName));
				httpURLConnection.getOutputStream().flush();
				outputStream = new ByteArrayOutputStream();
				inputStream = httpURLConnection.getInputStream();
				int len = 0;
				byte[] byteArray = new byte[1024 * 8];
				while ((len = inputStream.read(byteArray)) != -1) {
					outputStream.write(byteArray, 0, len);
				}
				outputStream.flush();
				byte[] data = outputStream.toByteArray();
				result = new String(data, charsetName);
			} catch (MalformedURLException e) {
				System.out.println("MalformedURLException 请求报文" + request+e);
			} catch (ProtocolException e) {
				System.out.println("ProtocolException 请求报文" + request+ e);
			} catch (IOException e) {
				System.out.println("IOException 请求报文" + request+e);
			} finally {
				if (inputStream != null) {
					try {
						inputStream.close();
					} catch (IOException e) {
						System.out.println("error happened close inputStream 请求报文"+ request+ e);
					}
				}
				if (outputStream != null) {
					try {
						outputStream.close();
					} catch (IOException e) {
						System.out.println("error happened close outputStream 请求报文"+ request+ e);
					}
				}
				httpURLConnection.disconnect();
			}
			System.out.println("响应报文" + result);
			return result;
		}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值