searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

将表单提交的body数据解析为map对象的方法实现

2024-11-13 09:32:04
4
0
public static Map parseFormDataParameter(String contentType, String formdata)  {
Map<String, String> formData = new HashMap<>();
//获取分隔符
String boundary = StringUtils.substringAfterLast(contentType,"boundary=");
boundary = "--" + boundary.trim();
BufferedReader reader = new BufferedReader(new StringReader(formdata));
//当前行的数据
String line;
//是否开始读取值数据
boolean startReadValue = false;
//参数名称
String key="";
//是否拼接换行符,首行不拼接
boolean needAppend = false;
//参数的值
StringBuilder value = new StringBuilder("");
while (true) {
try {
if ((line = reader.readLine()) == null){
//读取不到数据则退出
break;
}
} catch (IOException e) {
//解析异常则退出
break;
}

if(StringUtils.startsWith(line,boundary)){
//key不为空代表已经读完一个参数
if(StringUtils.isNotEmpty(key)){
//将已解析参数赋值
formData.put(key,value.toString());
//重置解析变量
startReadValue = false;
key="";
needAppend = false;
value = new StringBuilder("");
}
continue;
}

if(!line.trim().isEmpty() && line.startsWith("Content-Disposition:")){
//解析参数名称
key=StringUtils.substringBetween(line," name=\"","\"");
}else if(!startReadValue && line.trim().isEmpty()){
//判断到收个换行符,以下则为参数的值数据
startReadValue = true;
}else if(startReadValue){
if(needAppend){
value.append("\r\n");
}else {
needAppend = true;
}
value.append(line);
}
}

return formData;
}


public static void main(String[] args) {

Map xm = parseFormDataParameter("multipart/form-data; boundary=71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ", "--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ\r\n" +
"Content-Disposition: form-data; name=\"certifacation\"; filename=\"certifacation\"\r\n" +
"Content-Type: application/octet-stream\r\n" +
"Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"-----BEGIN CERTIFICATE-----\r\n" +
"MIID6jCCAtKgAwIBAgIQGo8ogoDJQ1qESOhKS4gD1TANBgkqhkiG9w0BAQsFADBe\r\n" +
"MQswCQYDVQQGEwJDTjEOMAwGA1UEChMFTXlTU0wxKzApBgNVBAsTIk15U1NMIFRl\r\n" +
"-----END CERTIFICATE-----\r\n" +
"--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ\r\n" +
"Content-Disposition: form-data; name=\"updateTimeEnd\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"2024-10-26 00:00\r\n" +
"--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ\r\n" +
"Content-Disposition: form-data; name=\"certifacationType\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"1\r\n" +
"--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ\r\n" +
"Content-Disposition: form-data; name=\"domain\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"abc.ctcdn.cn\r\n" +
"--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ\r\n" +
"Content-Disposition: form-data; name=\"keyType\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"1\r\n" +
"--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ\r\n" +
"Content-Disposition: form-data; name=\"updateTimeStart\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"2024-10-25 00:00\r\n" +
"--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ\r\n" +
"Content-Disposition: form-data; name=\"key\"; filename=\"key\"\r\n" +
"Content-Type: application/octet-stream\r\n" +
"Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"-----BEGIN RSA PRIVATE KEY-----\r\n" +
"MIIEpAIBAAKCAQEA4J7PB7SDCO0hQUOLDk7b3SL589gNA\r\n" +
"-----END RSA PRIVATE KEY-----\r\n" +
"--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ--");
System.out.println("form data " + xm);
}

--------------------------------------
执行完就输出解析后的map数据如下所示
form data {certifacation=-----BEGIN CERTIFICATE-----
MIID6jCCAtKgAwIBAgIQGo8ogoDJQ1qESOhKS4gD1TANBgkqhkiG9w0BAQsFADBe
MQswCQYDVQQGEwJDTjEOMAwGA1UEChMFTXlTU0wxKzApBgNVBAsTIk15U1NMIFRl
-----END CERTIFICATE-----, updateTimeEnd=2024-10-26 00:00, certifacationType=1, domain=abc.ctcdn.cn, keyType=1, updateTimeStart=2024-10-25 00:00, key=-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA4J7PB7SDCO0hQUOLDk7b3SL589gNA
-----END RSA PRIVATE KEY-----}
0条评论
作者已关闭评论
陈青龙
7文章数
0粉丝数
陈青龙
7 文章 | 0 粉丝
原创

将表单提交的body数据解析为map对象的方法实现

2024-11-13 09:32:04
4
0
public static Map parseFormDataParameter(String contentType, String formdata)  {
Map<String, String> formData = new HashMap<>();
//获取分隔符
String boundary = StringUtils.substringAfterLast(contentType,"boundary=");
boundary = "--" + boundary.trim();
BufferedReader reader = new BufferedReader(new StringReader(formdata));
//当前行的数据
String line;
//是否开始读取值数据
boolean startReadValue = false;
//参数名称
String key="";
//是否拼接换行符,首行不拼接
boolean needAppend = false;
//参数的值
StringBuilder value = new StringBuilder("");
while (true) {
try {
if ((line = reader.readLine()) == null){
//读取不到数据则退出
break;
}
} catch (IOException e) {
//解析异常则退出
break;
}

if(StringUtils.startsWith(line,boundary)){
//key不为空代表已经读完一个参数
if(StringUtils.isNotEmpty(key)){
//将已解析参数赋值
formData.put(key,value.toString());
//重置解析变量
startReadValue = false;
key="";
needAppend = false;
value = new StringBuilder("");
}
continue;
}

if(!line.trim().isEmpty() && line.startsWith("Content-Disposition:")){
//解析参数名称
key=StringUtils.substringBetween(line," name=\"","\"");
}else if(!startReadValue && line.trim().isEmpty()){
//判断到收个换行符,以下则为参数的值数据
startReadValue = true;
}else if(startReadValue){
if(needAppend){
value.append("\r\n");
}else {
needAppend = true;
}
value.append(line);
}
}

return formData;
}


public static void main(String[] args) {

Map xm = parseFormDataParameter("multipart/form-data; boundary=71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ", "--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ\r\n" +
"Content-Disposition: form-data; name=\"certifacation\"; filename=\"certifacation\"\r\n" +
"Content-Type: application/octet-stream\r\n" +
"Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"-----BEGIN CERTIFICATE-----\r\n" +
"MIID6jCCAtKgAwIBAgIQGo8ogoDJQ1qESOhKS4gD1TANBgkqhkiG9w0BAQsFADBe\r\n" +
"MQswCQYDVQQGEwJDTjEOMAwGA1UEChMFTXlTU0wxKzApBgNVBAsTIk15U1NMIFRl\r\n" +
"-----END CERTIFICATE-----\r\n" +
"--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ\r\n" +
"Content-Disposition: form-data; name=\"updateTimeEnd\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"2024-10-26 00:00\r\n" +
"--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ\r\n" +
"Content-Disposition: form-data; name=\"certifacationType\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"1\r\n" +
"--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ\r\n" +
"Content-Disposition: form-data; name=\"domain\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"abc.ctcdn.cn\r\n" +
"--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ\r\n" +
"Content-Disposition: form-data; name=\"keyType\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"1\r\n" +
"--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ\r\n" +
"Content-Disposition: form-data; name=\"updateTimeStart\"\r\n" +
"Content-Type: text/plain; charset=ISO-8859-1\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" +
"2024-10-25 00:00\r\n" +
"--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ\r\n" +
"Content-Disposition: form-data; name=\"key\"; filename=\"key\"\r\n" +
"Content-Type: application/octet-stream\r\n" +
"Content-Transfer-Encoding: binary\r\n" +
"\r\n" +
"-----BEGIN RSA PRIVATE KEY-----\r\n" +
"MIIEpAIBAAKCAQEA4J7PB7SDCO0hQUOLDk7b3SL589gNA\r\n" +
"-----END RSA PRIVATE KEY-----\r\n" +
"--71gQDsgER2PBL0zZjMbrup6rloH-8Y1wbJJ--");
System.out.println("form data " + xm);
}

--------------------------------------
执行完就输出解析后的map数据如下所示
form data {certifacation=-----BEGIN CERTIFICATE-----
MIID6jCCAtKgAwIBAgIQGo8ogoDJQ1qESOhKS4gD1TANBgkqhkiG9w0BAQsFADBe
MQswCQYDVQQGEwJDTjEOMAwGA1UEChMFTXlTU0wxKzApBgNVBAsTIk15U1NMIFRl
-----END CERTIFICATE-----, updateTimeEnd=2024-10-26 00:00, certifacationType=1, domain=abc.ctcdn.cn, keyType=1, updateTimeStart=2024-10-25 00:00, key=-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA4J7PB7SDCO0hQUOLDk7b3SL589gNA
-----END RSA PRIVATE KEY-----}
文章来自个人专栏
陈青龙个人
7 文章 | 1 订阅
0条评论
作者已关闭评论
作者已关闭评论
0
0