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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:57:45+00:00 2026-06-03T03:57:45+00:00

Using JAVA, I’m trying to force the browser to download files. Here is the

  • 0

Using JAVA, I’m trying to force the browser to download files.

Here is the code I currently use:

response.reset();
response.resetBuffer();
response.setContentType(mimeType);
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

InputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();
IOUtils.copy(in, out);

out.flush();
out.close();
in.close();
response.flushBuffer();

It works almost well, but when forcing the download of a docx document (MS Office 2007+), the downloaded file is corrupted (at least, that’s what MS Office tells me). If I try to open it directly on the server which stores them, that error doesn’t appear, which means that the problem does interfere when downloading (and not when uploading).

According to IANA, the MIME type of such a file should be application/vnd.openxmlformats-officedocument.wordprocessingml.document (1), but specifying that MIME type doesn’t resolve the problem.

There are several tracks on the Web, but none of them worked for me. There seems to be a solution in ASP.NET, but I didn’t find the equivalent in JAVA.

I also tried to use the MIME type application/vnd.ms-word (2) as I saw there, but the downloaded file is still corrupted. Idem for the MIME type application/msword (3) a guy suggested here, and for the generic MIME type application/octet-stream (4) as put forward on this forum.

Then, for each of these four MIME types, I tried to change the name of the downloaded file from myfile.docx to myfile.doc (no x anymore), but the problem persists.

So, how to force the download of an uncorrupted-when-downloaded docx file? Is my piece of code correct?

  • 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-03T03:57:46+00:00Added an answer on June 3, 2026 at 3:57 am

    I tried by chance to add more headers, and in fact, the Content-Length header resolved the problem…

    So finally, I just add this line to make it work:

    response.setContentLength((int) file.length());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using java code in windows i need to download several files from a directory
I´m using JAVA and I´m trying to extract or save images files from a
Using java I am trying to develop a method using recursion to analyze a
I'm using java, and I'm trying to make a JTextArea that is non-editable but
Using Java have the source code of a webpage stored in a string. I
Using java to connect to a mySQL database, if I use a update query
Using Java 1.4.2 with unlimited jurisdiction policy files installed. I have a class that
Using Java: I am reading a directory containing files with Greek Names. But when
Using Java over Windows Vista, I'm trying to create a kind of a monitor
Using Java I have created RSA keypairs. Using Java I can use these keys

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.