我正在使用REST API。我有下面的代码片段:
conn = (HttpURLConnection) url.openConnection();
OutputStream outputstream = conn.getOutputStream();
if (payload.getString("phase_id") != null) {
bodyParam = new JSONObject();
bodyParam.put("phase_id", payload.getString("phase_id"));
outputstream.write(bodyParam.toString().getBytes());
}
logger.info(""+outputstream);
outputstream.flush();
if (payload.getString("subphase_id") != null) {
bodyParam = new JSONObject();
bodyParam.put("subphase_id", payload.getString("subphase_id"));
outputstream.write(bodyParam.toString().getBytes());
}
logger.info(""+outputstream);在这里,即使我刷新了输出流,它也给出了如下输出:
{"phase_id":101}
{"phase_id":101}{"subphase_id":201}但是我想要的输出如下:
{"phase_id":101}
{"subphase_id":201}我担心的是,即使我在刷新数据,为什么它会给出以前的数据?是不是我漏掉了什么?
发布于 2020-07-31 19:16:12
它不会显示重复的数据,您只需记录两次。
Java OutputStream的
()方法将写入OutputStream的所有数据刷新到底层数据目的地。例如,如果OutputStream是FileOutputStream,则写入FileOutputStream的字节可能尚未完全写入磁盘。数据可能缓冲在OS内存中的某个位置,即使您的Java代码已将其写入FileOutputStream。通过调用flush(),您可以确保任何缓冲的数据都将被刷新(写入)到磁盘(或网络,或OutputStream的目标所具有的任何其他内容)。
link。我认为你想在再次写之前清除你的OutputStream,但这不是flush所做的。
https://stackoverflow.com/questions/63190152
复制相似问题