其他分享
首页 > 其他分享> > ftp多文件压缩下载

ftp多文件压缩下载

作者:互联网

@GetMapping(value = "/find")
public String findfile(String filePath, String fileNames, HttpServletResponse response) {
    initFtpClient();
    FtpUtils f = new FtpUtils();

    boolean b = false;
    try {
        b = f.downloadFile(filePath, fileNames,  response, ftpClient);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (b){
        return "下载成功";
    }else{
        return "下载失败";
    }

}
public boolean downloadFile(String pathname, String filenames, HttpServletResponse response, FTPClient ftpClient) throws IOException {
    boolean flag = false;

    //获取文件名数组
    String[] splitFiles = filenames.split(",");

    // 获取输出流
    BufferedOutputStream out = null;
    try {
        out = new BufferedOutputStream(response.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }


    try {
        System.out.println("开始下载文件");
        //   initFtpClient();
        //切换FTP目录
        ftpClient.enterLocalPassiveMode();
        ftpClient.changeWorkingDirectory(pathname);
        FTPFile[] ftpFiles = ftpClient.listFiles();

        response.reset();
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream;charset=utf-8");
        //设置输出压缩包名字
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("downloadFile.zip", "UTF-8"));

        //建立ZipOutputStream类:完成文件或文件夹的压缩
        ZipOutputStream zipOut = new ZipOutputStream(out);

        //循环要下载的文件名
        for (String filename : splitFiles) {
            //循环ftp服务器文件列表,判断文件是否存在
            for (FTPFile file : ftpFiles) {
                if (filename.equalsIgnoreCase(file.getName())) {

                    //获取ftp文件输入流
                    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                    InputStream inputStream = ftpClient.retrieveFileStream(file.getName());
                    byte[] bytes = IOUtils.toByteArray(inputStream);
                    InputStream in = new ByteArrayInputStream(bytes);
              
                    //创建压缩包中每个文件的文件名
                    zipOut.putNextEntry(new ZipEntry(filename));

                    byte[] buffer = new byte[1024 * 10];
                    int len;
                    try {
                        while ((len = in.read(buffer)) != -1) {
                            zipOut.write(buffer, 0, len);
                        }
                        zipOut.closeEntry();
                        //下面两行必须有,否则会导致inputStream空指针
                        inputStream.close();
                        ftpClient.completePendingCommand();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    flag = true;
                }
            }
        }
        zipOut.close();
        out.close();

        ftpClient.logout();

        System.out.println("下载文件成功");
    } catch (Exception e) {
 //       response.setContentType("text/plain;charset=utf-8");
   //     Integer error_code = 0;
   //     String real_msg = "下载错误";
   //     response.sendError(error_code, real_msg);
   //     response.setStatus(000);
        System.out.println("下载文件失败");
        e.printStackTrace();
    } finally {
        if (ftpClient.isConnected()) {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (null != out) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return flag;
}

 

标签:ftp,String,压缩,try,IOException,下载,response,ftpClient,out
来源: https://www.cnblogs.com/oeds/p/15762990.html