package com.yinxin.control;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.yinxin.entity.DfgzPkgMx;
import com.yinxin.tools.Log4jBean;
public class Test {
public void readFileContents(String LocalFilePath, int RecvPkgNo) {
Log4jBean.logger.info("进入readFileContents这个方法");
try {
// read file content from file
File file = new File(LocalFilePath);
Log4jBean.logger.info("读取文件的路径为[" + LocalFilePath + "]");
// BufferedReader br = new BufferedReader(new FileReader(file));
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "gbk"));
String str = null;
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:SS");
String nowTime = formatter.format(new Date());
Log4jBean.logger.info("需要拼接的系统时间为[" + nowTime + "]");
StringBuffer sb = new StringBuffer();
while ((str = br.readLine()) != null) {
sb.append(str + "\r\n");
}
Log4jBean.logger.info("文件内容为:\n" + sb);
List<DfgzPkgMx> list = new ArrayList<DfgzPkgMx>();
// 读取文件内容,进行内容分割后
Log4jBean.logger.info("开始进行按行[$]进行切割文件");
String[] splitFileContents = sb.toString().split("\\$");
for (int i = 0; i < splitFileContents.length - 1; i++) {
DfgzPkgMx mx = new DfgzPkgMx(); // 每循环一次就会开个空间
StringBuffer stringBuffer = new StringBuffer();
// stringBuffer.append(nowTime + "&"); // 拼接入库时间
// stringBuffer.append(RecvPkgNo + "&"); // 通知包号
// splitFileContents[i] = stringBuffer.toString()+
// splitFileContents[i]; // 重新赋值
String[] ss = splitFileContents[i].split("\\&"); // 将文件每行用&进行切割
// mx.setInTime(ss[0]);
mx.setRecvPkgNo(RecvPkgNo);
mx.setCode(ss[0]);
mx.setContractorCorpCode(ss[1]);
mx.setContractorCorpName(ss[2]);
mx.setPayBankCode(ss[3]);
mx.setPayBankName(ss[4]);
mx.setPayBankCardNumber(ss[5]);
mx.setPayFlag(ss[6]);
mx.setWorkerName(ss[7]);
mx.setIDCardType(ss[8]);
mx.setIDCardNumber(ss[9]);
mx.setPayRollBankCardNumber(ss[10]);
mx.setPayRollBankName(ss[11]);
mx.setPayRollTopBankName(ss[12]);
mx.setBankLinkNumber(ss[13]);
mx.setPayRollTopBankCode(ss[14]);
mx.setTotalPayAmount(Integer.parseInt(ss[15]));
mx.setBalanceDate(ss[16]);
mx.setPlatFlowNo(ss[17]);
mx.setSysNo(ss[18]);
mx.setPayRollCode(ss[19]);
list.add(mx);
}
Log4jBean.logger.info("取出拼接的第一条数据" + splitFileContents[0]);
// 遍历list集合,逐条入库
int count = 0; // 入库记录数
for (int j = 0; j < list.size(); j++) {
Log4jBean.logger.info("取出集合中对象中的值来逐条进行入库!");
DfgzPkgMx mx1 = new DfgzPkgMx();
mx1 = list.get(j);
// 逐条插入DFGZ_pkgmx工资包明细信息表中,其中“通知包号”字段值为DFGZ_package的通知包号
int temp = insertSplitFileContents(mx1);
if (temp == 0) {
Log4jBean.logger.info("数据已经写入成功,正准备更新状态!!!!");
count++; // 统计实际成功入库记录数
updatePkgState(mx1.getRecvPkgNo(), count);
} else {
// 插入失败时,置失败标志
Log4jBean.logger.error("数据写入失败,置失败标志[99]!");
updateInsertFailCWXX(RecvPkgNo);
}
}
br.close(); // 关闭字符流
} catch (FileNotFoundException e) {
Log4jBean.logger.error("读取文件异常,异常信息为[" + e.getMessage() + "]");
} catch (IOException e) {
Log4jBean.logger.error("IO异常,异常信息为[" + e.getMessage() + "]");
}
}
}
package com.string;
import java.util.Scanner;
public class Character_Judge {
public static void main(String[] args) {
System.out.println("请随机输入一段字符串");
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
char s[] = str.toCharArray();
int char_num=0;//计算字母
int num = 0;//计算数字
int other = 0;//计算其他字符
for(int i=0;i<s.length;i++)
{
if (s[i]<='9'&&s[i]>='0')
num++;
else if (s[i]>='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z')
char_num++;
else
other++;
}
System.out.println("字符有"+char_num+"个;数字有"+num+"个;其他字符有"+other+"个");
}
}class CustomerAccu extends AccumulatorV2[Int, Int] {
//定义一个属性
var sum: Int = 0
//判断是否为空
override def isZero: Boolean = sum == 0
//复制一个累加器
override def copy(): AccumulatorV2[Int, Int] = {
val accu = new CustomerAccu
accu.sum = this.sum
accu
}
//重置累计器
override def reset(): Unit = sum = 0
//累加值,在executor端执行的操作,用户调用
override def add(v: Int): Unit = sum += v
//合并结果,在Driver端进行合并
override def merge(other: AccumulatorV2[Int, Int]): Unit = {
this.sum += other.value
}
//对外暴露方法,得到返回值结果,用户调用
override def value: Int = sum
}object TestCustomerAccu {
def main(args: Array[String]): Unit = {
//1.创建SparkConf
val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("TestHBase")
//2.创建SparkContext
val sc = new SparkContext(sparkConf)
//3.定义一个变量
var accu = new CustomerAccu
//注册自定义累加器
sc.register(accu, "sum")
//4.创建一个RDD
val numRDD: RDD[Int] = sc.parallelize(Array(1, 2, 3, 4), 2)
numRDD.map(x => {
accu.add(x)
x
}).collect()
//打印自定义累加器的值
println(accu.value)
sc.stop()
}
}
运行结果:
例:将文本串中将李白的《静夜思》的各个部分分别提取出来,并格式化输出。标题加粗,文本居中对齐,诗歌正文颜色显示灰色
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script type="text/javascript">
var comment = "静夜思李白床前明月光,疑是地上霜。举头望明月,低头思故乡。";
var partial = comment.substring(0,3); //取出标题
partial = partial.bold(); //标题加粗
document.write("<p align=\"center\">");//输出HTML标签"<p>",并设置居中对齐
docunment.write(partial); //输出标题
partial = comment.slice(3,5); //取出作者
document.write("<br />"); //输出换行标签<br>
document.write(partial); //输出作者
partial = comment.slice(5,17); //取出第一句诗文
partial = partial.fontcolor("gray"); //设置颜色为灰色
document.write("<br />");
document.write(partial);
partial = comment.slice(17,29); //取出第二就诗文
partial = partial.fontcolor("gray"); //设置颜色为灰色
document.write("<br />");
document.write(partial);
document.write("</p>");
</script>
</body>
</html> <html>
<head>
<meta charset="utf-8" />
<title>显示转换</title>
</head>
<body>
<script type="text/javascript">
var priceOfApple = "3元";
var priceOfBanana = "3.5元";
priceOfApple = parseInt(priceOfApple);
var priceOfBanana2 = parseInt(priceOfBanana);
priceOfBanana = parseFloat(priceOfBanana);
if(priceOfApple == 3 && priceOfBanana2 == 3 && priceOfBanana == 3.5)
{
document.write(priceOfApple + priceOfBanana2 + priceOfBanana);
}
else
{
document.write("没有得到预期结果");
}
</script>
</body>
</html>
public static void main(String[] args) {
String host = "https://dm-58.data.aliyun.com";
String path = "/rest/160601/ocr/ocr_business_license.json";
String method = "POST";
String appcode = "你自己的AppCode";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
//根据API的要求,定义相对应的Content-Type
headers.put("Content-Type", "application/json; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
String bodys = "{\"image\":\"对图片内容进行Base64编码\"}";
try {
/**
* 重要提示如下:
* HttpUtils请从
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
* 下载
*
* 相应的依赖请参照
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
*/
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
System.out.println(response.toString());
//获取response的body
//System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e) {
e.printStackTrace();
}
}
{
"config_str" : "null\n", #配置字符串信息
"angle" : float, #输入图片的角度(顺时针旋转),[0, 90, 180,270]
"reg_num" : string, #注册号,没有识别出来时返回"FailInRecognition"
"name" : string, #公司名称,没有识别出来时返回"FailInRecognition"
"type" : string, #公司类型,没有识别出来时返回"FailInRecognition"
"person" : string, #公司法人,没有识别出来时返回"FailInRecognition"
"establish_date": string, #公司注册日期(例:证件上为"2014年04月16日",算法返回"20140416")
"valid_period": string, #公司营业期限终止日期(例:证件上为"2014年04月16日至2034年04月15日",算法返回"20340415")
#当前算法将日期格式统一为输出为"年月日"(如"20391130"),并将"长期"表示为"29991231",若证件上没有营业期限,则默认其为"长期",返回"29991231"。
"address" : string, #公司地址,没有识别出来时返回"FailInRecognition"
"capital" : string, #注册资本,没有识别出来时返回"FailInRecognition"
"business": string, #经营范围,没有识别出来时返回"FailInRecognition"
"emblem" : string, #国徽位置[top,left,height,width],没有识别出来时返回"FailInDetection"
"title" : string, #标题位置[top,left,height,width],没有识别出来时返回"FailInDetection"
"stamp" : string, #印章位置[top,left,height,width],没有识别出来时返回"FailInDetection"
"qrcode" : string, #二维码位置[top,left,height,width],没有识别出来时返回"FailInDetection"
"success" : bool, #识别成功与否 true/false
"request_id": string
}
package com.casic.cloud.qcy.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.alibaba.fastjson.JSONObject;
import com.casic.cloud.qcy.constant.Constants;
import sun.misc.BASE64Encoder;
/**
* @ClassName: AliyunImageRecognitionUtil
* @Description: 阿里云图像识别工具类
* @author: tianpengw
* @date 2019年3月21日 上午8:49:08
*
*/
public class AliyunImageRecognitionUtil {
private static String businessLicenceHost = PropertiesUtil.getProperties("businessLicenceHost");
private static String businessLicencePath = PropertiesUtil.getProperties("businessLicencePath");
private static String method = "POST";
private static String appCode = PropertiesUtil.getProperties("appCode");
/**
*
* @Description: 根据文件全路径识别
* @author: tianpengw
* @param imgPath
* @return
*/
public static Map<String,Object> bussinessLicenceRecognition(String imgPath){
Map<String,Object> resultMap = new HashMap<String,Object>();
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appCode);
headers.put("Content-Type", "application/json; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
String bodys = "{\"image\":\""+imageToBase64Str(imgPath)+"\"}";
try {
/**
* 重要提示如下:
* HttpUtils请从
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
* 下载
*
* 相应的依赖请参照
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
*/
HttpResponse response = AliyunHttpUtils.doPost(businessLicenceHost, businessLicencePath, method, headers, querys, bodys);
String resStr = EntityUtils.toString(response.getEntity());
System.out.println(resStr);
resultMap = JSONObject.parseObject(resStr, Map.class);
//获取response的body
} catch (Exception e) {
e.printStackTrace();
}
return resultMap;
}
/**
*
* @Description: 根据InputStream识别
* @author: tianpengw
* @param imgPath
* @return
*/
public static Map<String,Object> bussinessLicenceRecognition(InputStream inputStream){
Map<String,Object> resultMap = new HashMap<String,Object>();
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appCode);
headers.put("Content-Type", "application/json; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
// 加密
BASE64Encoder encoder = new BASE64Encoder();
byte[] data = null;
try {
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
String bodys = "{\"image\":\""+encoder.encode(data)+"\"}";
try {
/**
* 重要提示如下:
* HttpUtils请从
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
* 下载
*
* 相应的依赖请参照
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
*/
HttpResponse response = AliyunHttpUtils.doPost(businessLicenceHost, businessLicencePath, method, headers, querys, bodys);
String resStr = EntityUtils.toString(response.getEntity());
System.out.println(resStr);
resultMap = JSONObject.parseObject(resStr, Map.class);
resultMap.put("errCode", Constants.RESULT_SUCCESS);
//获取response的body
} catch (Exception e) {
e.printStackTrace();
resultMap.put("errCode", Constants.RESULT_FAIL);
}
return resultMap;
}
/**
*
* @Description: 根据byte[]识别
* @author: tianpengw
* @param imgPath
* @return
*/
public static Map<String,Object> bussinessLicenceRecognition( byte[] data){
Map<String,Object> resultMap = new HashMap<String,Object>();
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appCode);
headers.put("Content-Type", "application/json; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
// 加密
BASE64Encoder encoder = new BASE64Encoder();
String bodys = "{\"image\":\""+encoder.encode(data)+"\"}";
try {
/**
* 重要提示如下:
* HttpUtils请从
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
* 下载
*
* 相应的依赖请参照
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
*/
HttpResponse response = AliyunHttpUtils.doPost(businessLicenceHost, businessLicencePath, method, headers, querys, bodys);
String resStr = EntityUtils.toString(response.getEntity());
System.out.println(resStr);
resultMap = JSONObject.parseObject(resStr, Map.class);
resultMap.put("errCode", Constants.RESULT_SUCCESS);
//获取response的body
} catch (Exception e) {
e.printStackTrace();
resultMap.put("errCode", Constants.RESULT_FAIL);
}
return resultMap;
}
/**
* 图片转base64字符串
* @param imgFile 图片路径
* @return
*/
public static String imageToBase64Str(String imgFile) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(imgFile);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// 加密
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
public static void main(String[] args) {
String imgPath = "d:/yyzznew1.jpg";
Map<String,Object> map = AliyunImageRecognitionUtil.bussinessLicenceRecognition(imgPath);
System.out.println(map.get("person"));
System.out.println(map.get("reg_num"));
}
}