创建存放相应文件
String filepath = vars.get("RenderTradeContractFlow_resp_filepath");
log.info("----- file path :" + filepath);
//如果文件存在,则追加内容;如果文件不存在,则创建文件
File f = new File(filepath);
if(f.exists()){
f.delete();
} else {
File fileParent = f.getParentFile();
if(! fileParent.exists()){
fileParent.mkdirs();
}
}
f.createNewFile();
log.info("----- success create file :" + filepath);
${__setProperty("fpp", vars.get("RenderTradeContractFlow_resp_filepath"),)};
调用接口
// import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.charset.Charset;
import com.google.common.io.Files;
// import java.util.regex.Matcher;
// import java.util.regex.Pattern;
// import com.google.common.io.Files;
// import java.nio.charset.Charset;
if ("200".equals(prev.getResponseCode())) {
String last_response = prev.getResponseDataAsString();
JSONObject jO = JSONObject.parseObject(last_response).getJSONObject("res");
String canBuy = jO.getJSONObject("buyCanBuyTO").getString("canBuy");
if ("true".equals(canBuy)) {
Failure = false;
// FileWriter fw;
String filepath = ${__property("fpp")};
//如果文件存在,则追加内容;如果文件不存在,则创建文件
File f = new File(filepath);
try {
//对该文件加锁
RandomAccessFile out = new RandomAccessFile(f, "rw");
FileChannel fcout=out.getChannel();
FileLock flout=null;
while(true){
try {
flout = fcout.tryLock();
break;
} catch (Exception e) {
System.out.println("有其他线程正在操作该文件,当前线程休眠500毫秒");
Thread.sleep(500);
}
}
// String pattern = "(?<=\\"tradeContract\\").*(?=,\\s*\\"visibleTO\\")";
// Pattern r = Pattern.compile(pattern);
// Matcher m = r.matcher(str);
int le = "tradeContract\":".length();
int index=last_response.indexOf("tradeContract\":") + le;
String aaa = last_response.substring(index);
int index2 = aaa.lastIndexOf(",\"visibleTO\":");
// log.info("---------: " + index2);
String reqbody = "[" + aaa.substring(0,index2) + "]";
// JSONObject j = jO.getJSONObject("tradeContract");
// JSONArray ja22 = new JSONArray();
// ja22.add(j);
String req = vars.get("userid") + "^" + vars.get("cookies_gen") + "^" + reqbody + "^" + vars.get("mobile") + "\n";
// String req = ja22.toString();
// if (! req.endsWith("\n")) {
// req += "\n";
// }
// log.info(req);
// FileUtil.appendUtf8String(req, f);
Files.append(req,f,Charset.defaultCharset());
// FileUtil.appendString(req, f, "utf-8");
flout.release();
fcout.close();
out.close();
out=null;
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// FailureMessage = "执行成功" + renderTrade_resp;
// log.error("执行成功");
}