简体   繁体   English

使用Java Servlet多次下载

[英]multidownload using java servlets

I want to download multiple files in single zip file using java servlet. 我想使用Java Servlet在单个zip文件中下载多个文件。 I successfully downloaded the zip file containing multiple files (in my web page). 我成功下载了包含多个文件的zip文件(在我的网页中)。 but the problem is that files are also downloaded in my server(for ex in my jboss bin folder). 但是问题是文件也下载到了我的服务器中(例如,在我的jboss bin文件夹中)。

Code: 码:

public void createZipFile(String fileNames,HttpServletResponse response,HttpServletRequest request) {

        try {
            ZipOutputStream outStream1 = null;
            ServletOutputStream outStream=null;
            FileInputStream inStream =null;
            FileOutputStream fos=null;
            InputStream inputStream =null;
            int bytesRead = 0;


            String zipFileName = "zipFileName.zip";
            response.setHeader("Content-Disposition", "attachment;filename=\""+zipFileName+"\"");
            response.setHeader("Pragma", "private");
            response.addHeader("Cache-Control", "no-transform, max-age=0");
            response.setHeader("Accept-Ranges", "bytes");
            response.setContentType("application/zip");


            //fileNames contains multiple file name.so i want to split and get each file

            String[] fileName = fileNames.split(",");
            byte[] buffer = new byte[1024];
            outStream1 = new ZipOutputStream(new FileOutputStream(zipFileName));
            for(int i = 0; i < fileName.length; i++) {
                String filePath=audioFile.getAllFiles(fileName[i], Integer.parseInt(userId));
                inputStream = new URL("************************server File location***************************").openStream();
                String fileNaming = fileName[i];
                String[] tempFile = fileNaming.split("\\.");
                String tempFileExt=tempFile[1];
                String temporaryFile=tempFile[0]+"."+tempFileExt;



                fos = new FileOutputStream(temporaryFile);//here is the problem(temporary file created in my server -bin folder.but i dont want this to create)


                int length = -1;
                while ((length = inputStream.read(buffer)) > -1) {
                    fos.write(buffer, 0, length);
                }
                inStream = new FileInputStream(fileName[i]);
                outStream1.putNextEntry(new ZipEntry(fileName[i]));
                fos.close();
                inputStream.close();
                while ((bytesRead = inStream.read(buffer)) > 0) {
                    outStream1.write(buffer, 0, bytesRead);
                }

            }
            inStream.close();
            outStream1.closeEntry();

            outStream1.close();
            int bytesRead1 = 0;
            byte[] buff = new byte[1024];
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            FileInputStream inStream1 =new FileInputStream(zipFileName);
            while ((bytesRead1 = inStream1.read(buff)) != -1)
            {
                bao.write(buff, 0, bytesRead1);
            }

            byte[] videoBytes = bao.toByteArray();
            response.setContentLength(videoBytes.length);
            outStream = response.getOutputStream();
            outStream.write(videoBytes);
            outStream.flush();
            outStream.close();
            bao.close();
            response.flushBuffer();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

For whatever reason, you're copying the files twice. 无论出于何种原因,您都将文件复制了两次。 Once, to the file system via inputStream and fos , and the second time to the ZipFile, via inStream and outStream1 . 一次,通过inputStreamfos到文件系统,第二次通过inStreamoutStream1到ZipFile。

Seems like you can simply remove all of the code related to inputStream and fos , and you won't create the files on your filesystem. 似乎您可以简单地删除与inputStreamfos相关的所有代码,而不会在文件系统上创建文件。

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

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