From 0c0cdca38c3b05f6c83e1dd7979ae840debb8da9 Mon Sep 17 00:00:00 2001 From: nekolr Date: Sun, 12 Apr 2020 16:51:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=BE=93=E5=87=BA=20csv=20?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=A2=AB=E9=94=81=E5=AE=9A=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/executor/shape/OutputExecutor.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/spider-flow-core/src/main/java/org/spiderflow/core/executor/shape/OutputExecutor.java b/spider-flow-core/src/main/java/org/spiderflow/core/executor/shape/OutputExecutor.java index 4b6bff2..a9b9cd7 100644 --- a/spider-flow-core/src/main/java/org/spiderflow/core/executor/shape/OutputExecutor.java +++ b/spider-flow-core/src/main/java/org/spiderflow/core/executor/shape/OutputExecutor.java @@ -171,23 +171,28 @@ public class OutputExecutor implements ShapeExecutor, SpiderListener { String[] headers = data.keySet().toArray(new String[data.size()]); try { if (printer == null) { - CSVFormat format = CSVFormat.DEFAULT.withHeader(headers); - FileOutputStream os = new FileOutputStream(csvName); - String csvEncoding = node.getStringJsonValue(CSV_ENCODING); - if ("UTF-8BOM".equals(csvEncoding)) { - csvEncoding = csvEncoding.substring(0, 5); - byte[] bom = {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; - os.write(bom); - os.flush(); + synchronized (cachePrinter) { + printer = cachePrinter.get(key); + if (printer == null) { + CSVFormat format = CSVFormat.DEFAULT.withHeader(headers); + FileOutputStream os = new FileOutputStream(csvName); + String csvEncoding = node.getStringJsonValue(CSV_ENCODING); + if ("UTF-8BOM".equals(csvEncoding)) { + csvEncoding = csvEncoding.substring(0, 5); + byte[] bom = {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; + os.write(bom); + os.flush(); + } + OutputStreamWriter osw = new OutputStreamWriter(os, csvEncoding); + printer = new CSVPrinter(osw, format); + cachePrinter.put(key, printer); + } } - OutputStreamWriter osw = new OutputStreamWriter(os, csvEncoding); - printer = new CSVPrinter(osw, format); - cachePrinter.put(key, printer); } for (int i = 0; i < headers.length; i++) { records.add(data.get(headers[i]).toString()); } - synchronized (printer) { + synchronized (cachePrinter) { printer.printRecord(records); } } catch (IOException e) {