How to set UTF-8 encoding for file upload servlet

I have a servlet that sends a file to the browser.

I am sending these headers to the servlet.

if (request.isSecure()) {
            response.addHeader("Pragma", "no-cache");
            response.addHeader("Expires", "-1");
            response.addHeader("Cache-Control", "no-cache");
    } else {
            response.addHeader("Cache-Control", "private");
            response.addHeader("Pragma", "public");
    }

    if (isIE) {
            response.addHeader("Content-Disposition", "attachment; filename=\"" + encName + "\"" );
            response.addHeader("Connection", "close");
            response.setContentType("application/force-download; name=\"" + encName + "\"" );
    } else {
        response.addHeader("Content-Disposition", "attachment; filename=\"" + encName + "\"" );
        response.setContentType("application/octet-stream; name=\"" + encName + "\"" );
        if (contentLen > 0) {
            response.setContentLength(contentLen);
        }                        
    }

Then I send the file to the browser, but I have problems with the encoding of the file. The contents of the file are UTF-8, but I do not know how to send a header for this.

Does anyone have an idea how I can do this?

+5
source share
2 answers

There is no need to tell the browser that the file is UTF-8 encoded. By setting the content type to application / octet-stream, you indicate that the file should not be interpreted and may not be entirely textual.

, /- "text/plain; charset = utf-8".

+10

response.setCharacterEncoding( "UTF-8" );

+3

All Articles