Skip to content

Commit

Permalink
Merge pull request ssssssss-team#3 from nekolr/dev
Browse files Browse the repository at this point in the history
修复输出 csv 文件被锁定的问题
  • Loading branch information
javamxd authored Apr 12, 2020
2 parents 7b42f4c + 0c0cdca commit 1404f03
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,23 +171,28 @@ private void outputCSV(SpiderNode node, SpiderContext context, String csvName, M
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) {
Expand Down

0 comments on commit 1404f03

Please sign in to comment.