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

share|improve this question
1 
Does down_file.canRead() return true? – Xaerxess Aug 1 '12 at 16:01
yes, it returns true – Medioman92 Aug 1 '12 at 16:09

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.

share|improve this answer
i have another problem, i tried your code, but i'm still getting a NoClassDefFoundError relative to guava despite i have added the jar in the class path. P.S. the error come out only at running time, the java file doesn't notice any problem. Sorry for my bad english – Medioman92 Aug 2 '12 at 8:05
i solved class path problem, but anyway i'mm still getting 0 byte files when try to download – Medioman92Aug 2 '12 at 8:13
i've found where the problem is, File down_file = new File("log/" + file); with absolute path it works, with relative path it doesn't work, WHY ? – Medioman92 Aug 2 '12 at 8:39
What path does new File(".").getCanonicalPath() return? – Xaerxess Aug 2 '12 at 9:07
C:\Program Files\Apache Software Foundation\Tomcat 5.5\bin i solved using getServletContext().getRealPath("log")+file thanks – Medioman92 Aug 2 '12 at 9:32

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.

share|improve this answer
Thanx, this helped me as well. Although I've used Spring's FileCopyUtils to achieve the same – GileB Jan 25 at 11:45
@GileB glad to help :) – davioooh Jan 25 at 14:12



출처 - http://stackoverflow.com/questions/11762934/downloading-file-from-model-and-view-in-spring



Posted by linuxism
,