Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8605813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:55:52+00:00 2026-06-12T02:55:52+00:00

I use tomcat 5.5, JSF 1.2, Spring 3 I have the servlet that passes

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:55:53+00:00Added an answer on June 12, 2026 at 2:55 am

    You can use UTF-8 encoding

    String str = new String(data); //set your data in this object
    response.setContentType("text/plain");
    InputStream input = new ByteArrayInputStream(str.getBytes("UTF8"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to change Spring to use Datasource from Tomcat vs BasicDataSource? below is a
I use Tomcat 7 together with CDI and for that I used the jee6-servlet-minimal-archetype
I'm running a JSF 2.0 application on Tomcat 7. I want to use javascript
I use Tomcat 7 on Windows XP. I have a directory with static files
We use Tomcat for our java web application. There is a properties file under
I would like to seal and protect my WAR file, especially the JSP/JSF from
suppose, I use Tomcat as a web container. Is it true that once all
I'm running two webapps (based on Spring, JPA and JSF) on the same tomcat
i want to use EL 2.2 for JSF 2 application and tomcat 7 is
I use tomcat-maven-plugin to deploy my war to a server. What I have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.