java 从模型下载文件并在春季查看

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11762934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:17:04  来源:igfitidea点击:

Downloading file from model and view in spring

javaspringspring-mvcdownload

提问by Medioman92

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

我正在尝试创建一个页面,用户可以在其中下载一些 .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;
}

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

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

回答by Xaerxess

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.copycomes from wonderful Google's Guava library.

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

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 ;)):

此外,如果您使用的是 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));
    }

}

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

并在您的servlet.xml添加转换器中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 @Controllermethod annotated with @ResponseBody. Read more hereand here.

这样你就可以byte[]从任何@Controller@ResponseBody. 在这里这里阅读更多。

Files.toByteArrayis also from Guava.

Files.toByteArray也来自番石榴。

回答by davioooh

Try with:

尝试:

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

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

你可以在这里找到 Apache Commons IO:http: //commons.apache.org/io/

Hereyou find IOUtils.copy()reference.

在这里你可以找到IOUtils.copy()参考。