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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:46:47+00:00 2026-06-08T20:46:47+00:00

i’m trying to create a page where users can download some .log file. this

  • 0

i’m trying to create a page where users can download some .log file. this is the code :

if(action.equalsIgnoreCase("download")){
       String file = (String)request.getParameter("file");
       response.setHeader("Content-Disposition",
       "attachment;filename="+file+"");
       response.setContentType("text/plain");

       File down_file = new File("log/"+file);

       FileInputStream fileIn = new FileInputStream(down_file);
       ServletOutputStream out = response.getOutputStream();

       byte[] outputByte = new byte[4096];
       //copy binary contect to output stream
       while(fileIn.read(outputByte, 0, 4096) != -1)
       {
        out.write(outputByte, 0, 4096);
       }
       fileIn.close();
       out.flush();
       out.close();

       return null;
}

where am i doing wrong ?
when i click on the download button it correctly ask me to save the file, but it is always a 0 byte file…

  • 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-08T20:46:49+00:00Added an answer on June 8, 2026 at 8:46 pm

    This should do the job:

    public void getFile(final HttpServletResponse response) {
      String file = (String) request.getParameter("file");
      response.setHeader("Content-Disposition",
                         "attachment;filename=" + file);
      response.setContentType("text/plain");
    
      File down_file = new File("log/" + file);
      FileInputStream fileIn = new FileInputStream(down_file);
      ByteStreams.copy(fileIn, response.getOutputStream());
      response.flushBuffer();
    
      return null;
    }
    

    where ByteStreams.copy comes from wonderful Google’s Guava library.

    EDIT:

    Also, if you are using Spring MVC 3.1 you can do it in cleaner way (that’s how I do it, turns out to be one-liner ;)):

    @Controller
    public final class TestController extends BaseController {
    
        @RequestMapping(value = "/some/url/for/downloading/files/{file}",
                        produces = "text/plain")
        @ResponseBody
        public byte[] getFile(@PathVariable final String file) throws IOException {
            return Files.toByteArray(new File("log/" + file));
        }
    
    }
    

    and in your servlet.xml add converter to mvc:message-converters:

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
        </mvc:message-converters>
    </mvc:annotation-driven>
    

    so that you can return byte[] from any @Controller method annotated with @ResponseBody. Read more here and here.

    Files.toByteArray is also from Guava.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to create an if statement in PHP that prevents a single post

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.