Class FileAttachment

java.lang.Object
cloud.opencode.base.email.attachment.FileAttachment
All Implemented Interfaces:
Attachment

public final class FileAttachment extends Object implements Attachment
File-based Email Attachment 基于文件的邮件附件

Attachment implementation for local file system files.

本地文件系统文件的附件实现。

Features | 主要功能:

  • Load from Path - 从Path加载
  • Auto-detect MIME type - 自动检测MIME类型
  • Lazy loading - 延迟加载

Usage Examples | 使用示例:

Attachment attachment = new FileAttachment(Path.of("report.pdf"));
Email email = Email.builder()
    .attach(attachment)
    .build();

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Immutable: Yes - 不可变: 是
Since:
JDK 25, opencode-base-email V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • FileAttachment

      public FileAttachment(Path path)
      Create file attachment from path 从路径创建文件附件
      Parameters:
      path - the file path | 文件路径
    • FileAttachment

      public FileAttachment(Path path, String fileName)
      Create file attachment with custom file name 创建带自定义文件名的文件附件
      Parameters:
      path - the file path | 文件路径
      fileName - the display file name | 显示的文件名
    • FileAttachment

      public FileAttachment(Path path, String fileName, String contentType)
      Create file attachment with custom file name and content type 创建带自定义文件名和内容类型的文件附件
      Parameters:
      path - the file path | 文件路径
      fileName - the display file name | 显示的文件名
      contentType - the MIME type | MIME类型
  • Method Details

    • of

      public static FileAttachment of(Path path)
      Create file attachment from path 从路径创建文件附件
      Parameters:
      path - the file path | 文件路径
      Returns:
      the file attachment | 文件附件
    • of

      public static FileAttachment of(String path)
      Create file attachment from path string 从路径字符串创建文件附件
      Parameters:
      path - the file path string | 文件路径字符串
      Returns:
      the file attachment | 文件附件
    • getFileName

      public String getFileName()
      Description copied from interface: Attachment
      Get attachment file name 获取附件文件名
      Specified by:
      getFileName in interface Attachment
      Returns:
      the file name | 文件名
    • getContentType

      public String getContentType()
      Description copied from interface: Attachment
      Get attachment MIME type 获取附件MIME类型
      Specified by:
      getContentType in interface Attachment
      Returns:
      the MIME type | MIME类型
    • getInputStream

      public InputStream getInputStream()
      Description copied from interface: Attachment
      Get attachment data as input stream 获取附件数据输入流
      Specified by:
      getInputStream in interface Attachment
      Returns:
      the input stream | 输入流
    • getSize

      public long getSize()
      Description copied from interface: Attachment
      Get attachment size in bytes 获取附件大小(字节)
      Specified by:
      getSize in interface Attachment
      Returns:
      the size in bytes | 字节大小
    • getPath

      public Path getPath()
      Get the file path 获取文件路径
      Returns:
      the path | 路径
    • toString

      public String toString()
      Overrides:
      toString in class Object