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 9020635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:04:14+00:00 2026-06-16T05:04:14+00:00

I had tried to develop a servlet that allow user to download file but

  • 0

I had tried to develop a servlet that allow user to download file but it allow user to download the file but the file content contains binary garbage and not human readable. May I know what could be the reason ?

Code

int length = -1, index = 0;
        byte[] buffer = null;
        String attachmentPath = null, contentType = null, extension = null; 
        File attachmentFile = null;
        BufferedInputStream input = null;
        ServletOutputStream output = null;
        ServletContext context = null;

        attachmentPath = request.getParameter("attachmentPath");
        if (attachmentPath != null && !attachmentPath.isEmpty()) {
            attachmentFile = new File(attachmentPath);

            if (attachmentFile.exists()) {
                response.reset();

                context = super.getContext();  
                contentType = context.getMimeType(attachmentFile.getName());       
                response.setContentType(contentType);

                response.addHeader("content-length", String.valueOf(attachmentFile.length()));  
                response.addHeader("content-disposition", "attachment;filename=" + attachmentFile.getName());

                try {
                    buffer = new byte[AttachmentTask.DEFAULT_BUFFER_SIZE];
                    input = new BufferedInputStream(new FileInputStream(attachmentFile));
                    output = response.getOutputStream();

                    while ((length = input.read(buffer)) != -1) {
                        output.write(buffer, 0, length);
                        index += length;

//                      output.write(length);
                    }

                    output.flush();

                    input.close();
                    output.close();

                } catch (FileNotFoundException exp) {
                    logger.error(exp.getMessage());
                } catch (IOException exp) {
                    logger.error(exp.getMessage());
                }


            } else {
                try {
                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
                } catch (IOException exp) {
                    logger.error(exp.getMessage());
                }
            }

It is relate to writing file as binary or text mode or browser settings?

Please help.

Thanks.

  • 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-16T05:04:15+00:00Added an answer on June 16, 2026 at 5:04 am

    The problem is not in the code given so far. You’re properly using InputStream/OutputStream instead of a Reader/Writer to stream the file.

    The cause of the problem is more likely in the way how you created/saved the file. This problem will manifest when you’ve used a Reader and/or Writer which is not been instructed to use the proper character encoding for the characters being read/written. Perhaps you’re creating an upload/download service and the fault was in the upload process itself?

    Assuming that the data is in UTF-8, you should have created the reader as follows:

    Reader reader = new InputStreamReader(new FileInputStream(file), "UTF-8"));
    

    and the writer as follows:

    Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
    

    But if you actually don’t need to manipulate the stream on a per-character basis, but just wanted to transfer the data unmodified, then you should actually have used InputStream/OutputStream all the time.

    See also:

    • Unicode – How to get the characters right?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had tried using $('meta[name=description]').attr('content', 'new value'); But the above is not working in
i had tried to develop a web service using eclipse indigo but there is
I had more database and I tried to make backup for them but they
i have tried looking online but had no luck, How i could delete all
I've tried in 2 forums, but I had no luck so far. So, I
Last night I tried to put together something that I have had working since
Had tried following code in Linux, but always return 'C' under different LANG settings.
I had tried to create the library for iphone OS 4, but was getting
I had tried to use alert, prompt and result: If I open html file
I was wondering if anyone else has tried to develop a PHP extension that

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.