Java Servlet File Name With Special Characters Is Not Showing Correctly In File Download Dialog

April 13, 2017 503 views
Java

When trying to download the files using the java servlets with special characters in filenames like "昨夜のコンサートは最高でthúy ngọc.xlsx" is showing the boxes in file download dialog for IE11 and Firefox 45 browsers. Please do help in this regard.

1 Answer

Hi @sreenivasulu

Is your entire project and database UTF-8 or UTF-16 enabled all the way?
And make sure the webpage also defines charset.

This usually happens when some places supports UTF, but other places define another charset.

  • Thank you @hansen . We are using the UTF-8 charset in webpages as well as in database. Here is my scenario. The filename is saving at the database table column. From plsql procedure we are building the URL like http://<servler>/filedownload?filename=<filename_from_table_column>. The framed url we are showing as link in the webpage. when user hits , it takes to the servlet and there we are getting the filename from the request parameter. Please find below how content header is setting.

    encodedFilename = URLEncoder.encode(request.getParameter("filename"), "UTF-8").replace("+", "%20");

    if (Firefox) {
    response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFilename);
    } else if (IE || Chrome) {
    response.setHeader("Content-Disposition", "attachment; filename=" + encodedFilename);
    } else {
    response.setHeader("Content-Disposition",
    "attachment; filename=\"" + request.getParameter("filename") + "\"");
    }
    The file name not showing correctly.

Have another answer? Share your knowledge.