Medioman92
Medioman92

Reputation: 571

Downloading file from model and view in spring

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...

Upvotes: 0

Views: 14212

Answers (2)

Grzegorz Rożniecki
Grzegorz Rożniecki

Reputation: 27995

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.

Upvotes: 5

davioooh
davioooh

Reputation: 24666

Try with:

IOUtils.copy(fileIn, response.getOutputStream());
response.flushBuffer();

you can find Apache Commons IO here: http://commons.apache.org/io/

Here you find IOUtils.copy() reference.

Upvotes: 2

Related Questions