笔记
java原生包发送http请求
public static JSONObject sendRequest(String urlParam, String requestType) throws IOException {
HttpURLConnection con = null;
BufferedReader buffer = null;
OutputStreamWriter writer = null;
InputStream inputStream = null;
JSONObject resultData = new JSONObject();
StringBuffer resultBuffer = null;
try{
URL url = new URL(urlParam);
//得到连接对象
con = (HttpURLConnection) url.openConnection();
//设置请求类型
con.setRequestMethod("POST");
//设置请求需要返回的数据类型和字符集类型
con.setRequestProperty("Content-Type", "application/json");
//允许写出、读入
con.setDoOutput(true);
con.setDoInput(true);
//post请求不使用缓存
con.setUseCaches(false);
//设置body内的参数,put到JSONObject中
JSONObject param = new JSONObject();
param.put("app_id", "11");
param.put("app_key", "22");
// 建立实际的连接
con.connect();
// 得到请求的输出流对象
writer = new OutputStreamWriter(con.getOutputStream(),"UTF-8");
writer.write(param.toString());
writer.flush();
//通过响应码判断是否成功
if (con.getResponseCode()== HttpURLConnection.HTTP_OK) {
//得到响应流
inputStream = con.getInputStream();
//将响应流转换成字符串
resultBuffer = new StringBuffer();
String line;
buffer = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
while ((line = buffer.readLine()) != null) {
resultBuffer.append(line);
}
}
resultData.parseObject(resultBuffer.toString());
String str = (String) resultData.get("data");
return resultData.parseObject(resultBuffer.toString());
} catch (Exception e) {
} finally {
buffer.close();
writer.close();
inputStream.close();
}
return null;
}
get请求封装请求体
使用
广义上get没有请求体,当我们使用这个 HttpURLConnection 封装get请求body时,源码中会自动将get请求转为post请求。
解决方法时:通过反射将对象的参数 requestMethod 重置为get。
源码分析文献
2
//将post请求转为get请求(通过反射)
// 解决在Rest请求中,GET请求附带body导致GET请求被强制转换为POST
// 在sun.net.www.protocol.http.HttpURLConnection.getOutputStream0方法中,会把GET方法
// 修改为POST,而且无法调用setRequestMethod方法修改,因此此处使用反射强制修改字段属性值
// https://stackoverflow.com/questions/978061/http-get-with-request-body/983458
List<Field> fields = new ArrayList<>() ;
Class tempClass = con.getClass();
while (tempClass != null) {//当父类为null的时候说明到达了最上层的父类(Object类).
fields.addAll(Arrays.asList(tempClass.getDeclaredFields()));
tempClass = tempClass.getSuperclass(); //得到父类,然后赋给自己
}
for (Field field : fields) {
if ("method".equals(field.getName())){
field.setAccessible(true);
field.set(con,"GET");
}
}
writer.write(param.toString());
writer.flush();
mysql一条sql语句可以做批量插入,且含有事务(自带),后面出现错误,后面的会rollback。
decimal插入时不能插 “”,但是能插入NULL。
INSERT INTO students
(project,sumtransactionamt) VALUES(‘’,1),(‘aaa’,‘1’),(1,NULL)
正则表达式:
判断2022-01-12
str.matches("^\\d{4}-\\d{2}-\\d{2}
/**
* 返回当前日期与指定日期之间的差值
*
* @param startDate
* @return
*/
private String getDateDiffer(String startDate) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
String systemDate = formatter.format(date);
long startDateTime = formatter.parse(startDate).getTime();
long endDateTime = formatter.parse(systemDate).getTime();
int result = (int) ((endDateTime - startDateTime) / (1000 * 3600 * 24));
result = result % 1000 + 1;
String resultString = String.valueOf(result);
if (resultString.length() == 1) {
return "00" + resultString;
} else if (resultString.length() == 2) {
return "0" + resultString;
}
return resultString;
}
//获取上月的最后一天
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
int month = calendar.get(Calendar.MONTH);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
lastDate = sf.format(calendar.getTime());
//当前日期 yyyy-MM-dd
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
newDate = formatter.format(date);
增强for循环在遍历list时无法获取当前index
错误日志:
一、解析excel单元格为纯数字时,利用getStringCellValue()会报错,类型错误。
解决方法:
使用 org.apache.poi.xssf.usermodel 包下的 XSSFWorkbook 等
XSSFCell cell6 = row.getCell(columnIndexMap.get("Credit Product Code"));
cell6.setCellType(CellType.STRING);
String cproductcode =cell6.getStringCellValue();
二、HSSFWorkbook
使用org.apache.poi.hssf.usermode包下的 HSSFWorkbook
、HSSFSheet 读取excel文件发生版本错误,错误信息:The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
解决办法:
(1)判断文件后缀名是xls,还是xlsx
(2)如果是xls,使用HSSFWorkbook;如果是xlsx,使用XSSFWorkbook
三、sql插入字段过长
java.sql.BatchUpdateException: Data truncation: Data too long for column ‘backvalueflag’ at row 1
第一种情况就是很普遍的,xxx字段长度不够,
这里我设置的是33(varchar),结果存储不进去一个 ‘N’,原因是最后一个单元格有长字段描述