FTP多文件下载


 @RequestMapping("/downloadFiles")
 public void downloadFiles(HttpServletResponse response,HttpServletRequest request,String ids) {
  
   try {
     OutputStream out = null;
    //先获取路径信息
    String []idss = ids.split(",");
    List<Map<String,Object>> list = this.service.downloadFiles(idss);
    FTPUtil aFTPUtil = new FTPUtil();
    byte[] temp = new byte[4096]; 
//    byte[] bytes = new byte[4096];
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(bao);
    //根据路径去取流
    for(int i = 0; i < list.size(); i++){
     aFTPUtil.connectServer("10.0.6.181", 21, "Administrator", "123456", "/FTPServer");
     temp = aFTPUtil.downloadFileByName((String)list.get(i).get("FILE_NAME"));
     ZipEntry ze = null;
     ze = new ZipEntry((String)list.get(i).get("FILE_NAME"));
     ze.setSize(temp.length);
     ze.setTime(System.currentTimeMillis());
     zos.putNextEntry(ze);
     zos.write(temp);
    }
     zos.close();
     response.setContentType("application/x-msdownload");
              //发送html消息头
              response.setHeader("Content-Disposition", "attachment;filename="
                     + new String(("file.zip").getBytes("gbk"), "iso-8859-1"));
     out = response.getOutputStream();
     out.write(bao.toByteArray());
           out.flush();
           out.close();
   } catch (Exception e) {
    e.printStackTrace();
    log.error(e.getMessage());
   }
 }

 

 

 

  /**
     * 将指定的远程文件数据写到bytes
     *
     * @param remoteFileName
     * @param out
     * @throws Exception
     */
    public byte[] downloadFileByName(String remoteFileName) throws Exception {
        InputStream inputStream = null;
     try {
         if(StringUtils.isBlank(remoteFileName)){
             return null;
         }
         String foundName = null;
         FTPFile[] files = ftpClient.listFiles();
         String ftpFileName = "";
         for (FTPFile file : files) {
          ftpFileName = file.getName();
          if (file.isFile()
              && remoteFileName.equals(ftpFileName)) {
              foundName = file.getName();
              break;
          }
         }
         if(foundName != null && !foundName.equals("")){
          String encodedFoundFileName = CommonUtil.getString(foundName,
              CoreConstants.ENCODING_GBK, CoreConstants.ENCODING_ISO8859_1);
          inputStream = ftpClient.retrieveFileStream(encodedFoundFileName);
          if(inputStream==null){
              throw new Exception("Fail to download file from ftp server.");
          }
          log.debug("inputStream.available():"+inputStream.available());
          byte[] rtnDataArr = CommonUtil.inputStream2Bytes(inputStream);
          if(!ftpClient.completePendingCommand()) {
              throw new Exception("File transfer failed!Fail to download file from ftp server.");
          }
         
          return rtnDataArr;
         }
         return null;
     } catch (Exception e) {
         log.error("download file failed.", e);
         throw new Exception(e);
     } finally {
         try {
             if(inputStream!=null){
                 inputStream.close();
             }
         } catch (IOException e1) {
             inputStream = null;
         }
         try {
         closeServer();
         } catch (Exception e) {
         }
     }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值