Quantcast
Channel: Active questions tagged utf-8 - Stack Overflow
Viewing all articles
Browse latest Browse all 1154

Java Spring returning CSV file encoded in UTF-8 with BOM

$
0
0

Apparently for excel to open CSV files nicely, it should have the Byte Order Mark at the start. The download of CSV is implemented by writing into HttpServletResponse's output stream in the controller, as the data is generated during request. I get an exception when I try to write the BOM bytes - java.io.CharConversionException: Not an ISO 8859-1 character: [] (even though the encoding I specified is UTF-8).


The controller's method in question

@RequestMapping("/monthly/list")public List<MonthlyDetailsItem> queryDetailsItems(        MonthlyDetailsItemQuery query,        @RequestParam(value = "format", required = false) String format,        @RequestParam(value = "attachment", required = false, defaultValue="false") Boolean attachment,        HttpServletResponse response) throws Exception {       // load item list    List<MonthlyDetailsItem> list = detailsSvc.queryMonthlyDetailsForList(query);    // adjust format    format = format != null ? format.toLowerCase() : "json";    if (!Arrays.asList("json", "csv").contains(format)) format = "json";    // modify common response headers    response.setCharacterEncoding("UTF-8");    if (attachment)        response.setHeader("Content-Disposition", "attachment;filename=duomenys."+ format);    // build csv    if ("csv".equals(format)) {        response.setContentType("text/csv; charset=UTF-8");        response.getOutputStream().print("\ufeff");        response.getOutputStream().write(buildMonthlyDetailsItemCsv(list).getBytes("UTF-8"));        return null;    }    return list;}

Viewing all articles
Browse latest Browse all 1154

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>