main
package com.alone.main;
import com.alone.pojo.base.CurlParams;
import com.alone.util.FileUtil;
import com.alone.util.ResolveCurl;
import java.io.*;
/**
* @Author: alone
* @Date: 2021/12/4 12:15
*/
public class DemoMain {
public static void main(String[] args) throws IOException {
String path = "/Users/maoyan/work/curl.txt";
String curl = new FileUtil().getCurl(path);
ResolveCurl rc = new ResolveCurl(curl);
CurlParams cp = rc.getParams();
System.out.println(cp.getUrl());
System.out.println(cp.getData());
}
}
定义curl对象
package com.alone.pojo.base;
import lombok.Data;
import java.util.Map;
/**
* @Author: alone
* @Date: 2021/11/7 10:41
*/
@Data
public class CurlParams {
public String url;
public Map<String,String> header;
private String data;
}
解析curl到对象
/**
* @Author: alone
* @Date: 2021/11/6 17:30
* 解析curl
*/
public class ResolveCurl {
public String curl;
public ResolveCurl(String curl){
this.curl = curl;
}
public CurlParams getParams(){
String[] a = curl.split("\\n");
CurlParams cp = new CurlParams();
HashMap<String,String> h = new HashMap<>();
for(int j = 0; j<a.length;j++){
if(a[j].contains("curl")){
String url = a[j].replace("curl"," ").replace('\'',' ').replace('\\',' ').trim();
cp.setUrl(url);
}else if(a[j].contains("-H")){
String s = a[j].replace("-H"," ").replace('\'',' ').replace('\\',' ').trim();
h.put(s.split(": ")[0],s.split(": ")[1].trim());
}else if(a[j].contains("--data-raw")){
String data = a[j].replace("--data-raw"," ").replace('\'',' ').replace('\\',' ').trim();
cp.setData(data);
}
}
cp.setHeader(h);
return cp;
}
}
读取txt文件的curl
package com.alone.util;
import java.io.*;
import java.nio.charset.StandardCharsets;
/**
* @Author: alone
* @Date: 2021/12/4 16:41
*/
public class FileUtil {
public String getCurl(String path) throws IOException {
String content = "";
StringBuilder builder = new StringBuilder();
File file = new File(path);
InputStreamReader streamReader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(streamReader);
while ((content = bufferedReader.readLine()) != null) {
builder.append(content+"\n");
}
return builder.toString();
}
}