简体   繁体   English

如何从输入流文件中获取输入流?

[英]how to get input stream from input stream file?

I have been developing a java web app and I want to add a download function. 我一直在开发Java Web应用程序,并且想要添加下载功能。 I want to download zip file located in "C:\\apache-tomcat-6.0.36\\webapps\\xml\\XML.zip". 我想下载位于“ C:\\ apache-tomcat-6.0.36 \\ webapps \\ xml \\ XML.zip”中的zip文件。 I have converted the file to InputStream, but I'm still confused how to get input stream data from the InputStream? 我已经将文件转换为InputStream,但是我仍然很困惑如何从InputStream获取输入流数据?

When I click the download button, it returns 0 (zero) byte of the zip file 当我单击下载按钮时,它将返回zip文件的0(零)字节

Here is the controller to handle download zipfile : 这是处理下载zipfile的控制器:

@RequestMapping("download")
public String Download(HttpServletResponse response) {

    ZipInputStream zis = null;

    try {           

        InputStream is = new FileInputStream("C:\\apache-tomcat-6.0.36\\webapps\\xml\\XML.zip");
        zis = new ZipInputStream(is);

        response.setHeader("Content-Disposition", "inline;filename=\"" + "XML.zip" + "\"");
        OutputStream out = response.getOutputStream();
        response.setContentType("application/zip");
        IOUtils.copy(zis.getInputStream, out);
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    } 

    return null;
}

this line is causing the zero byte zip file: 这行导致零字节的zip文件:

IOUtils.copy(**zis.getInputStream**, out); 

Assuming that your code compiles: 假设您的代码可以编译:

If you are already picking up a zip file, there is no need to pass it through ZipInputStream again. 如果您已经在提取一个zip文件,则无需再次通过ZipInputStream传递它。

something like this http://www.avajava.com/tutorials/lessons/how-do-i-serve-up-a-pdf-from-a-servlet.html 像这样的东西http://www.avajava.com/tutorials/lessons/how-do-i-serve-up-a-pdf-from-a-servlet.html

If you want to get the whole ZIP file downloaded, you don't have to use ZipInputStream ... That is for accessing the contents of the ZIP file... 如果要下载整个ZIP文件,则不必使用ZipInputStream ...用于访问 ZIP文件的内容 ...

Instead of zis.getInputStream() use is.getInputStream() , and remove the code related to the ZipInputStream : 代替zis.getInputStream()使用is.getInputStream() ,并删除与ZipInputStream相关的代码:

@RequestMapping("download")
public String Download(HttpServletResponse response) {

  //ZipInputStream zis = null; no need for this

  try {           

    InputStream is = new FileInputStream("C:\\apache-tomcat-6.0.36\\webapps\\xml\\XML.zip");
    //zis = new ZipInputStream(is); //no need for this

    response.setHeader("Content-Disposition", "inline;filename=\"" + "XML.zip" + "\"");
    OutputStream out = response.getOutputStream();
    response.setContentType("application/zip");
    IOUtils.copy(is, out); //no zis here, and "is" is already an InputStream instance
    out.flush();
    out.close();
  } catch (IOException e) {
    e.printStackTrace();
  } 

  return null;
}

Also, I'd revise the .close() calls: they are almost always best fit for finally blocks to ensure everzthing gets closed properly. 另外,我还要修改.close()调用:它们几乎总是最适合finally块,以确保一切正常关闭。 (that, or try-with-resource blocks are to be used.) (即或尝试使用资源块。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM