I use tomcat 5.5, JSF 1.2, Spring 3
I have the servlet that passes file from disk to browser. The problem occures when that file has a text/html mime type.
I can’t know what encoding that file might have so I can’t set correct response encoding.
That’s the code of servlet
private void handleFILERequest(final FacesContext context) throws UnsupportedEncodingException {
String filePath = AbstractBean.getStrRequestScopeAttribute(FILE_PATH);
String mimeType = AbstractBean.getStrRequestScopeAttribute(FILE_MIME_TYPE);
String fileName = AbstractBean.getStrRequestScopeAttribute(FILE_NAME);
byte[] data = getFile(filePath);
HttpServletResponse response = AbstractBean.getResponse();
response.reset();
response.setContentType(mimeType);
response.setContentLength(data.length);
if (fileName == null || "".equals(fileName)) {
response.addHeader("Content-Disposition", "attachment; filename=\"downloadFile\"");
} else {
response.addHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
}
try {
response.getOutputStream().write(data);
} catch (Exception exception) {
LOG.error(exception.getMessage());
}
context.responseComplete();
}
private byte[] getFile(final String path) {
return IOUtils.readFile(path);
}
That problem occurs only when mime type of a file is text/html. Somehow that byte stream is re-encoded after I pass it to response outputstream. Also the html tag is slightly changed as you can see below. I think that servlet container do that but I am not sure.
Is there a way to detect file encoding to set it as response encoding or at least to prevent further re-encoding of response stream?
At least I’d like to know who changes that byte stream, tomcat, spring, jsf or…?
Here come a part of file on disk and resulting downloaded file in browser:
File on disk (cyrillic symbols, but no encoding defined):
<html>
<head>
<link HREF="/vestnik/csstyles/article.css" REL="stylesheet">
<title>Л.О. Бутакова. Опыт классификации ошибок ...</title>
</head>
...
File that I get in browser:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link HREF="/vestnik/csstyles/article.css" REL="stylesheet">
<title>пїЅ.пїЅ. пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ. пїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅ ...</title>
</head>
...
Thanks in advance.
You can use UTF-8 encoding