我们面临的问题是,我们从必须使用Java处理的服务中收到了一个“损坏的” csv。
我们通过一次简单的交流来接收csv:
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
String data = responseEntity.getBody();
并像这样解析它:
import com.univocity.parsers.common.processor.RowListProcessor;
import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
...
private RowListProcessor createCSVParserFromString(String data) throws IOException {
RowListProcessor rowProcessor = new RowListProcessor();
CsvParserSettings settings = new CsvParserSettings();
settings.setUnescapedQuoteHandling(STOP_AT_CLOSING_QUOTE);
settings.getFormat().setLineSeparator("\n");
settings.getFormat().setDelimiter(';');
settings.setProcessor(rowProcessor);
settings.setHeaderExtractionEnabled(true);
settings.getFormat().setComment('∞');
CsvParser parser = new CsvParser(settings);
parser.parse(new ByteArrayInputStream(data.getBytes()), "UTF-8");
return rowProcessor;
}
但是,只要我们没有正确地转义csv中的内容,这似乎就无法解决。
一个匿名示例,打破了csv:
#sys_id;name;install_status;sub_status;sys_created_on;sys_updated_on;owned_by;u_deputies;u_grant_permission_ag;assignmentgroup;u_organisational_entity;u_bs_short_description;u_alternative_name;short_description;u_number_of_impacted_user;u_release_number;u_resp_mgr_amos_op_al;u_resp_mgr_amos_op_dep;u_platform_portal;u_type_of_application;u_solvency_ii_relevance;department;u_resp_mgr_bo_dep;u_resp_mgr_fach;u_database_system;u_legitimation_process;u_accounting_relevance;u_kpmg_cluster;busines_criticality;u_crit_periods_of_time;u_inter_extra_intranet;u_operation_time;u_name_mail_addr_oe_cpi;u_service_time;u_recovery_time_sla;u_203_relevance;u_bia_date;u_business_impact_analysis_existing;u_bdsg_relevance;u_health_information;u_icofr_relevance;u_licence_type;u_vip;u_classification_confidentiality;u_classification_integrity;u_arcsight;u_network_zone;u_datasafe_relevance;u_rpo;u_rto;u_status_wap;u_last_review;u_overall_application_status
"809a2106008ce3c0e485e5ca44fedba5";"WebserviceConverter_T";"Active";"Ready";"22.08.2018 09:36:37";"04.12.2018 14:43:40";"some name";"some name";"true";"something";"something";"Der "WebserviceConverter" ist ein SOAP-Service, Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Die Anwendung wurde im Rahmen des Projekts "1015646 Abl�sung Netviewer durch Citrix" f�r den Hardwarebestelldialog entwickelt, Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
";"";"https://google.de/WebserviceConverter";"";"";"";"";"SVCI";"Business application";"false";"Vertriebsportale";"asd-123-asd";"";"";"";"false";"";"2 - critical";"N/A";"Intranet";"Mo-Fr: 06:30-20 Uhr; Sa: 08:00 - 16:00 Uhr";"asdf.IT-123.123-PB";"";"";"false";"";"No";"false";"false";"";"";"";"";"";"";"";"false";"";"";"";"04.12.2018 14:43:40";""
是否有一种自动方法来正确解析此类行?