报错:常量字符串过长
作者:互联网
原因分析:常量字符串的长度不能长于65535 - 1字节;
由于图片通过接口传递,图片转为base64编码后,字符串很大,故yreport的值很大,有360800个字符。
原代码:
public class DataEncrypt2Test { public static void main(String[] args) throws Exception { String pubKey = "省略不写"; // 生产批次 String requestData = "{\n" + "\t\"ycocode\": \"09876543211234567890\",\n" + "\t\"yconame\": \"华润制药\",\n" + "\t\"medicode\": \"B1243253\",\n" + "\t\"mediname\": \"当归\",\n" + "\t\"allowname\": \"张山\",\n" + "\t\"locality\": \"深圳\",\n" + "\t\"batchno\": \"B345353\",\n" + "\t\"batchciid\": \"3452352\",\n" + "\t\"prodate\": \"2021-10-10\",\n" + "\t\"checkdate\": \"2021-11-11\",\n" + "\t\"packgg\": \"10/箱\",\n" + "\t\"checkname\": \"王五\",\n" + "\t\"norm\": \"国标\",\n" + "\t\"arts\": \"工艺01\",\n" + "\t\"yreport\": \"此处为超长字符串,省略不写\",\n" + "\t\"updatetime\": \"2021-12-12\",\n" + "\t\"ybatchid\": \"1232141\",\n" + "\t\"quantity\": \"100\",\n" + "\t\"unit\": \"kg\",\n" + "\t\"billtype\": \"销售出库\"\n" + "}"; String encrypt = RSAUtils.encryptByPublicKey(requestData, pubKey); System.out.println("requestData===> "); System.out.println(encrypt); } }
解决办法:使用StringBuilder,将字符串分成多段进行拼接
public class DataEncrypt2Test { public static void main(String[] args) throws Exception { String pubKey = "省略不写";// 生产批次 StringBuilder sb = new StringBuilder(); sb.append("很长字符串1,省略不写,下同"); sb.append("很长字符穿2"); sb.append("很长字符串3"); sb.append("很长字符串4"); sb.append("很长字符串5"); sb.append("很长字符串6"); sb.append("很长字符串7" ); sb.append("很长字符串8"); sb.append("很长字符串9"); sb.append("很长字符串10"); sb.append("很长字符串11"); String str = sb.toString(); String requestData = "{\n" + "\t\"ycocode\": \"09876543211234567890\",\n" + "\t\"yconame\": \"众益制药\",\n" + "\t\"medicode\": \"B1243253\",\n" + "\t\"mediname\": \"当归\",\n" + "\t\"allowname\": \"张山\",\n" + "\t\"locality\": \"深圳\",\n" + "\t\"batchno\": \"B345353\",\n" + "\t\"batchciid\": \"3452352\",\n" + "\t\"prodate\": \"2021-10-10\",\n" + "\t\"checkdate\": \"2021-11-11\",\n" + "\t\"packgg\": \"10/箱\",\n" + "\t\"checkname\": \"王五\",\n" + "\t\"norm\": \"国标\",\n" + "\t\"arts\": \"工艺01\",\n" + "\t\"yreport\": \""+ str +"\",\n" + "\t\"updatetime\": \"2021-12-12\",\n" + "\t\"ybatchid\": \"1232141\",\n" + "\t\"quantity\": \"100\",\n" + "\t\"unit\": \"kg\",\n" + "\t\"checkname\": \"王五\",\n" + "\t\"billtype\": \"销售出库\"\n" + "}"; String encrypt = RSAUtils.encryptByPublicKey(requestData, pubKey); System.out.println("requestData===> "); System.out.println(encrypt); } }
标签:10,常量,过长,很长,报错,字符串,sb,append,String 来源: https://www.cnblogs.com/zwh0910/p/15703535.html