3

My downloaded file name becomes Ça_r_lar_02_07_2019_12_09.xlsx, however, I want it Çağrılar_02_07_2019_12_09.xlsx. How Can I fix it?

try (Workbook workbook = new XSSFWorkbook()) {
                new XlsExporter().exportXls(workbook, grh);
                SimpleDateFormat sdf = new SimpleDateFormat("_dd_MM_yyyy_HH_mm");
                String name = grh.getReportName() + sdf.format(new Date());
                response.setContentType(MediaType.APPLICATION_OCTET_STREAM.getType());
                response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + name +  ".xlsx\"");
                workbook.write(response.getOutputStream());
                response.getOutputStream().flush();
            }
Yryskul Turduev
  • 197
  • 1
  • 11
  • And you are sure that the underlying file system (operating system) supports these characters in file names?! – GhostCat Jul 02 '19 at 09:26
  • @GhostCat Yes, it supports – Yryskul Turduev Jul 02 '19 at 09:28
  • Have you tried coding your name variable directly with the corresponding unicode charcater as shown here? : https://stackoverflow.com/questions/5585919/creating-unicode-character-from-its-number/5598346 For example your c should be u00c7 – ItFreak Jul 02 '19 at 09:29

1 Answers1

3

Try UTF-8 encoding for your filename before sending the response

try (Workbook workbook = new XSSFWorkbook()) {
                new XlsExporter().exportXls(workbook, grh);
                SimpleDateFormat sdf = new SimpleDateFormat("_dd_MM_yyyy_HH_mm");
                String name = grh.getReportName() + sdf.format(new Date());
                name = URLEncoder.encode(name,"UTF-8"); 
                response.setContentType(MediaType.APPLICATION_OCTET_STREAM.getType());
                response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + name +  ".xlsx\"");
                workbook.write(response.getOutputStream());
                response.getOutputStream().flush();
            }
Alex
  • 878
  • 1
  • 10
  • 25
  • This is correct, as by the HTTP standard the header lines sent before the actual content are in ISO-8859-1. – Joop Eggen Jul 02 '19 at 10:57
  • blank becomes "+" character, do u know why it is? – Yryskul Turduev Jul 02 '19 at 11:24
  • 1
    That is because of URL encoding. You can append ```.replaceAll("\\+", "%20")``` to your URLEncoder function. Or use URI class (or URIUtils in Spring) which use RFC 2396. More info can be found here: https://en.wikipedia.org/wiki/Percent-encoding – Alex Jul 02 '19 at 11:42