public static int parseCSV(InputStream input){
// BufferedReader in = new BufferedReader(new InputStreamReader(input));
File fstream = File.createTempFile("csv",".txt");
FileOutputStream out = new FileOutputStream(fstream);
IOUtils.copy(input, out);
out.close();
List<CSVRecord> recordList;
try(BufferedInputStream bf = new BufferedInputStream(new FileInputStream(fstream.getAbsoluteFile()));
BufferedReader in = new BufferedReader(new InputStreamReader(bf));)
{
CSVParser parser = CSVFormat
.EXCEL
.withIgnoreEmptyLines(true)
.withIgnoreSurroundingSpaces(false)
.withRecordSeparator(System.getProperty("line.separator"))
.withDelimiter(',')
.withEscape('\\')
.withQuote('"')
.parse(in);
recordList = parser.getRecords();
}
catch(Exception e){
logger.error("retry parse tmpfile:" + fstream.getAbsoluteFile(),e );
BufferedInputStream bf = new BufferedInputStream(new FileInputStream(fstream.getAbsoluteFile()));
BufferedReader in = new BufferedReader(new InputStreamReader(bf,"UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
line.replaceAll("\\\\", "");
line = line.replaceAll("(?!(^|,|\",))\"(?!$|\")","\\\\\"");
line = line.replaceAll(",\\\\\"",",\"");
sb.append(line+System.getProperty("line.separator"));
}
CSVParser parser = CSVFormat
.EXCEL
.withIgnoreEmptyLines(true)
.withIgnoreSurroundingSpaces(false)
.withRecordSeparator(System.getProperty("line.separator"))
.withDelimiter(',')
.withEscape('\\')
.withQuote('"')
.parse(new StringReader(sb.toString()));
recordList = parser.getRecords();
bf.close();
in.close();
}
fstream.deleteOnExit();
//以下、略