在春天从模型和视图下载文件

Med*_*n92 0 java spring spring-mvc download

我正在尝试创建一个用户可以下载某个.log文件的页面.这是代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

我在哪里做错了?当我点击下载按钮时它正确地要求我保存文件,但它总是一个0字节的文件...

Xae*_*ess 5

这应该做的工作:

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;
}
Run Code Online (Sandbox Code Playgroud)

其中,ByteStreams.copy来自于美妙的谷歌的番石榴库.

编辑:

此外,如果你使用的是Spring MVC 3.1,你可以用更干净的方式(我就是这样做,结果是单行;)):

@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));
    }

}
Run Code Online (Sandbox Code Playgroud)

并在您的servlet.xml添加转换器中mvc:message-converters:

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
    </mvc:message-converters>
</mvc:annotation-driven>
Run Code Online (Sandbox Code Playgroud)

这样您就可以byte[]从任何@Controller带注释的方法返回@ResponseBody.在这里这里阅读更多.

Files.toByteArray 也来自番石榴.