标签:ftp String 压缩 try IOException 下载 response ftpClient out
@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
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。