压缩从 SFTP 服务器下载的文件

发布于 2024-12-03 14:16:13 字数 198 浏览 1 评论 0原文

我正在开发一个项目,需要从 SFTP 服务器下载 2 个文件,使用时间戳名称将它们压缩并将其保存到我的本地服务器。另外,一旦检索到文件,我希望它发送一封电子邮件,表明它成功完成了作业或失败了。我正在使用 JSch 检索 SFTP 服务器,并且可以将文件保存在我的本地服务器中。谁能建议我应该如何编写代码来压缩文件并发送电子邮件。感谢您的帮助,我正在使用 Java 来完成这个小项目。

I am working on a project where I need to download 2 files from an SFTP server, zip them up with a timestamp name and save it to the my local server. Also, once the files are retrieved I want it to send an email that either it successfully completed the job or failed. I am using JSch for retrieveing the SFTP server and could save the files in my local server. Could anyone suggest how should I write my codes to zip the files and send an email. Any help appreciated and I am working with Java for this small project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老娘不死你永远是小三 2024-12-10 14:16:18

使用 ZipOutputStreamJava Mail 您应该能够执行您的操作想。我创建了一个示例 main 方法,它将主机、from 和 to 作为命令行参数。它假设您已经知道压缩文件名并发送一封附有 zip 的电子邮件。我希望这有帮助!

public class ZipAndEmail {
  public static void main(String args[]) {
    String host = args[0];
    String from = args[1];
    String to = args[2];

    //Assuming you already have this list of file names
    String[] filenames = new String[]{"filename1", "filename2"};

    try {
      byte[] buf = new byte[1024];
      String zipFilename = "outfile.zip";
      ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename));

      for (int i=0; i<filenames.length; i++) {
        FileInputStream in = new FileInputStream(filenames[i]);
        out.putNextEntry(new ZipEntry(filenames[i]));
        int len;
        while ((len = in.read(buf)) > 0) {
          out.write(buf, 0, len);
        }
        out.closeEntry();
        in.close();
      }
      out.close();


      Properties props = System.getProperties();
      props.put("mail.smtp.host", host);
      Session session = Session.getDefaultInstance(props, null);

      // Define message
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(from));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
      message.setSubject("Your Zip File is Attached");

      MimeBodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setText("Your zip file is attached");
      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(messageBodyPart);

      // Part two is attachment
      messageBodyPart = new MimeBodyPart();
      DataSource source = new FileDataSource(zipFilename);
      messageBodyPart.setDataHandler(new DataHandler(source));
      messageBodyPart.setFileName(zipFilename);
      multipart.addBodyPart(messageBodyPart);

      // Put parts in message
      message.setContent(multipart);

      // Send the message
      Transport.send( message );

      // Finally delete the zip file
      File f = new File(zipFilename);
      f.delete();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Using ZipOutputStream and Java Mail you should be able to do what you want. I created a sample main method that takes in the host, from and to as command line arguments. It assumes you already know the zipped filenames and sends an email with the zip attached. I hope this helps!

public class ZipAndEmail {
  public static void main(String args[]) {
    String host = args[0];
    String from = args[1];
    String to = args[2];

    //Assuming you already have this list of file names
    String[] filenames = new String[]{"filename1", "filename2"};

    try {
      byte[] buf = new byte[1024];
      String zipFilename = "outfile.zip";
      ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename));

      for (int i=0; i<filenames.length; i++) {
        FileInputStream in = new FileInputStream(filenames[i]);
        out.putNextEntry(new ZipEntry(filenames[i]));
        int len;
        while ((len = in.read(buf)) > 0) {
          out.write(buf, 0, len);
        }
        out.closeEntry();
        in.close();
      }
      out.close();


      Properties props = System.getProperties();
      props.put("mail.smtp.host", host);
      Session session = Session.getDefaultInstance(props, null);

      // Define message
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(from));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
      message.setSubject("Your Zip File is Attached");

      MimeBodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setText("Your zip file is attached");
      Multipart multipart = new MimeMultipart();
      multipart.addBodyPart(messageBodyPart);

      // Part two is attachment
      messageBodyPart = new MimeBodyPart();
      DataSource source = new FileDataSource(zipFilename);
      messageBodyPart.setDataHandler(new DataHandler(source));
      messageBodyPart.setFileName(zipFilename);
      multipart.addBodyPart(messageBodyPart);

      // Put parts in message
      message.setContent(multipart);

      // Send the message
      Transport.send( message );

      // Finally delete the zip file
      File f = new File(zipFilename);
      f.delete();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文