欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

把java做成linux脚本(类似小工具)

程序员文章站 2022-07-08 15:55:22
...

java 最轻便的部署方式

D:\2020工作计划\2020-09-08\New Folder\checke-chai-data>

  1. 目录结构如下
  2. lib 是放编译好的class 文件的依赖
  3. readFile 不用关心
  4. .sh 是脚本文件

把java做成linux脚本(类似小工具)图一

HttpsPostRequestWithClientKey这个类必须放在根目录下(src 目录下, 放在默认包下)

把java做成linux脚本(类似小工具)图二

在工程目录下找到该java 文件的编译文件(.class文件)复制到图一目录

把java做成linux脚本(类似小工具)图三

HttpsPostRequestWithClientKey这个类所依赖的jar 包需要放在图1 的lib 目录下

这样其实就已经可以跑起来了, 如果在window 环境可以直接在图1目录栏输入cmd 进入命令窗口启动程序
进入

把java做成linux脚本(类似小工具)图四

score 是参数, 可选
java -cp “.:./lib/" HttpsPostRequestWithClientKey score(windows)
放入文件 .bat 文件中就可以双击运行
java -cp ".;./lib/
” HttpsPostRequestWithClientKey score (linux)

放入.sh 就可以 ./ 运行了

下面贴出我的HttpsPostRequestWithClientKey工具类

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;
import javax.sound.sampled.Line;

import org.aspectj.weaver.ast.Var;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class HttpsPostRequestWithClientKey
{
/**
* 请求超时时间
*/
private static final int TIME_OUT = 120000;

/**
 * Https请求
 */
private static final String HTTPS = "https";

/**
 * Content-Type
 */
private static final String CONTENT_TYPE = "Content-Type";

/**
 * 表单提交方式Content-Type
 */
private static final String FORM_TYPE = "application/x-www-form-urlencoded;charset=UTF-8";

/**
 * JSON提交方式Content-Type
 */
private static final String JSON_TYPE = "application/json;charset=UTF-8";

/**
 * 获取服务器信任
 */
private static void getTrust2()
{
	try
	{
		HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier()
		{

			public boolean verify(String hostname, SSLSession session)
			{
				return true;
			}
		});

		String PFX_PATH = "/home/wsnblockchain/go/src/github.com/hyperledger/fabric/fabric-samples/balance-transfer/server-cert/client2.p12";
		String PFX_PWD = "wsn123";
		KeyStore keyStore = KeyStore.getInstance("PKCS12");
		InputStream instream = new FileInputStream(new File(PFX_PATH));
		try
		{
			// 这里就指的是KeyStore库的密码
			keyStore.load(instream, PFX_PWD.toCharArray());
		} finally
		{
			instream.close();
		}
		KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
		kmf.init(keyStore, PFX_PWD.toCharArray());

		SSLContext context = SSLContext.getInstance("SSL");

		context.init(kmf.getKeyManagers(), new X509TrustManager[]
		{ new X509TrustManager()
		{

			public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException
			{
			}

			public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException
			{
			}

			public X509Certificate[] getAcceptedIssuers()
			{
				return new X509Certificate[0];
			}
		} }, new SecureRandom());
		HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
	} catch (Exception e)
	{
		e.printStackTrace();
	}
}

private static Response doPostRequest(String url, Map<String, String> headers, String jsonParams) throws IOException
{
	if (null == url || url.isEmpty())
	{
		throw new RuntimeException("The request URL is blank.");
	}

	// 如果是Https请求
	if (url.startsWith(HTTPS))
	{
		getTrust2();
	}

	Connection connection = Jsoup.connect(url);
	connection.method(Method.POST);
	connection.timeout(TIME_OUT);
	connection.ignoreHttpErrors(true);
	connection.ignoreContentType(true);
	connection.maxBodySize(0);

	if (null != headers)
	{
		connection.headers(headers);
	}

	connection.header(CONTENT_TYPE, JSON_TYPE);
	connection.requestBody(jsonParams);

	Response response = connection.execute();
	return response;
}

public static Response post(String url, String params) throws IOException
{
	return doPostRequest(url, null, params);
}
/**
 * 仓押入链https 访问方式示例
 * @param args
 */
public static void main(String[] args)
{
	if (args.length == 0)
	{
		System.out.println("请输入查询业务类型");
		return;
	}
	System.out.println(args[0]);
	String idName  = null;
	String url = null;
	String requestType = null;
	String fileName = null;
	// data : [["1", '仓押ID'], ["2", '企业开工ID'], ["3", '企业评分ID']]
	if(args[0].equals("ware")){
		url = "ware/queryWareData";
		requestType = "WarehouseQueryRequest";
		idName = "orderNo";
		fileName = "wareIds";
	}else if(args[0].equals("working")){
		url = "query/EnterpriseWorking";
		requestType = "EnterpriseWorkingRequest";
		idName = "dataId";
		fileName = "workingIds";
	}else{
		url = "query/EnterpriseScore";
		requestType = "EnterpriseScoreRequest";
		idName = "dataId";
		fileName = "scoreIds";
	}
	File file = new File("/home/wsnblockchain/go/src/github.com/hyperledger/fabric/fabric-samples/balance-transfer/checke-chai-data/readFile/" + fileName + ".txt");
	FileReader reader = null;
	BufferedReader bufferedReader = null;
	try
	{
		reader = new FileReader(file);
		bufferedReader = new BufferedReader(reader);
		try
		{
			String dataId = null;
			while ((dataId = bufferedReader.readLine()) != null)
			{
				Response post;
				String strURL = "https://192.168.11.139:4000/" + url;
				String paramJsonString = "{\"" + requestType +"\":{\"BODY\":{\""+ idName +"\":\"" + dataId  +"\"}}}";
				System.out.println("请求参数" + JSON.toJSON(paramJsonString));
				try
				{
					post = post(strURL, paramJsonString);
					System.out.println("响应参数" + post.body());
				} catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	} catch (FileNotFoundException e1)
	{
		e1.printStackTrace();
	}finally{
		if (reader != null)
		{
			try
			{
				reader.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		if (bufferedReader != null)
		{
			try
			{
				bufferedReader.close();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
}

}

相关标签: java