ทำงานกับไฟล์ vCard (VCF) ใน Java

บทความนี้ครอบคลุม VCardContact คลาสที่อ่านและเขียนไฟล์ vCard (VCF) โดยอิสระจาก Outlook และ MAPI เพื่อสร้างและจัดการ Outlook MapiContact รายการ — ที่สามารถส่งออกเป็น VCF ได้ — ดู ทำงานกับรายชื่อ Outlook.

VCardContact กับ MapiContact

Aspose.Email เปิดเผยข้อมูลติดต่อผ่านสองคลาสที่แตกต่างกัน การเลือกคลาสที่เหมาะสมจะช่วยหลีกเลี่ยงความสับสนจำนวนมาก

  • VCardContact — วัตถุ vCard แท้ ใช้เมื่อแหล่งและเป้าหมายเป็นไฟล์ vCard (VCF) และไม่ต้องการฟีเจอร์ Outlook/MAPI รองรับการอ่านและเขียน VCF โดยตรง รองรับ vCard 2.1/3.0/4.0 และหลายรายการต่อไฟล์
  • MapiContact — รายการติดต่อ Outlook ใช้เมื่อทำงานกับ MSG/PST หรือจำเป็นต้องใช้คุณสมบัติ MAPI MapiContact ยังสามารถนำเข้าและส่งออกเป็น VCF ผ่าน fromVCard และ save วิธี (ดู ทำงานกับรายชื่อ Outlook).

ส่วนที่เหลือของบทความนี้เน้นที่ VCardContact.

สร้าง vCard และบันทึกมัน

นี้ VCardContact คลาสมีคอนสตรัคเตอร์โดยไม่มีพารามิเตอร์และเปิดเผยชุดคุณสมบัติแบบ strongly‑typed เช่น VCardIdentificationInfo, VCardOrganization, VCardEmailCollection, VCardTelephoneNumberCollection, และ VCardDeliveryAddressCollection.

โค้ดตัวอย่างต่อไปนี้แสดงวิธีสร้างข้อมูลติดต่อตั้งแต่ต้นและบันทึกเป็นไฟล์ vCard (VCF)

VCardContact contact = new VCardContact();

VCardFullName fullName = new VCardFullName();
fullName.setGivenName("Bertha");
fullName.setMiddleName("A.");
fullName.setFamilyName("Buell");

VCardIdentificationInfo identificationInfo = new VCardIdentificationInfo();
identificationInfo.setDisplayName("Bertha Buell");
identificationInfo.setFullName(fullName);
identificationInfo.setNickname("Bertie");
identificationInfo.setBirthday(new GregorianCalendar(1980, java.util.Calendar.MAY, 20).getTime());
contact.setIdentificationInfo(identificationInfo);

VCardOrganization organization = new VCardOrganization();
organization.setOrganization("Awthentikz");
organization.setTitle("Social work assistant");
contact.setOrganization(organization);

VCardEmail email = new VCardEmail();
email.setEmailAddress("BerthaABuell@example.com");
email.setEmailType(VCardEmailType.INTERNET);
contact.getEmails().addItem(email);

// Save as a vCard (VCF) file
contact.save("contact.vcf");

โมเดลคุณสมบัติของข้อมูลติดต่อ

นอกเหนือจากชื่อและอีเมลที่แสดงด้านบนแล้ว VCardContact เปิดเผยโมเดลข้อมูล vCard ทั้งหมดผ่านชุดคุณสมบัติแบบ strongly‑typed ตัวอย่างโค้ดด้านล่างขยาย contact สร้างก่อนหน้านี้

ที่อยู่ไปรษณีย์

ที่อยู่ไปรษณีย์อยู่ใน VCardDeliveryAddressCollection เป็น VCardDeliveryAddress รายการ ประเภทที่อยู่รับค่าการผสมของ VCardDeliveryAddressType แฟล็ก (HOME, WORK, POSTAL, PARCEL, DOM, INTL, PREF).

VCardContact contact = new VCardContact();

VCardDeliveryAddress address = new VCardDeliveryAddress();
address.setAddressType(VCardDeliveryAddressType.WORK | VCardDeliveryAddressType.PREF);
address.setStreet("4 Darwinia Loop");
address.setLocality("Eighty Mile Beach");
address.setRegion("WA");
address.setPostalCode("6725");
address.setCountryName("Australia");

contact.getDeliveryAddresses().addItem(address);

หมายเลขโทรศัพท์

หมายเลขโทรศัพท์อยู่ใน VCardTelephoneNumberCollection เป็น VCardTelephoneNumber รายการ ประเภทโทรศัพท์รับการผสมผสานของ VCardTelephoneType แฟล็ก (VOICE, WORK, HOME, CELL, FAX, PAGER, PREF, และอื่นๆ)

VCardContact contact = new VCardContact();

VCardTelephoneNumber workPhone = new VCardTelephoneNumber();
workPhone.setTelephoneNumber("(08) 9080 1183");
workPhone.setTelephoneType(VCardTelephoneType.WORK | VCardTelephoneType.VOICE);

VCardTelephoneNumber mobilePhone = new VCardTelephoneNumber();
mobilePhone.setTelephoneNumber("(925) 599 3355");
mobilePhone.setTelephoneType(VCardTelephoneType.CELL);

contact.getTelephoneNumbers().addItem(workPhone);
contact.getTelephoneNumbers().addItem(mobilePhone);

ตำแหน่งภูมิศาสตร์

นี้ Geo คุณสมบัติเก็บตำแหน่งทั่วโลกเป็น float ละติจูดและลองจิจูด

VCardContact contact = new VCardContact();

VCardGeo geo = new VCardGeo();
geo.setLatitude(48.858844f);
geo.setLongitude(2.294351f);

contact.setGeo(geo);

Photo

รูปถ่ายของข้อมูลติดต่อถูกตั้งค่าใน IdentificationInfo.Photo เป็น VCardPhoto. สามารถฝังในบรรทัดจากไบต์ของภาพหรืออ้างอิงด้วย URL รูปแบบภาพกำหนดโดย VCardPhotoType และโหมดการจัดเก็บโดย VCardValueLocation.

VCardContact contact = new VCardContact();
contact.setIdentificationInfo(new VCardIdentificationInfo());

// Embed the image bytes inline
VCardPhoto inlinePhoto = new VCardPhoto();
inlinePhoto.setData(Files.readAllBytes(Paths.get("portrait.jpg")));
inlinePhoto.setPhotoType(VCardPhotoType.JPEG);
inlinePhoto.setValueLocation(VCardValueLocation.INLINE);
contact.getIdentificationInfo().setPhoto(inlinePhoto);

// Or reference an external image by URL
VCardPhoto linkedPhoto = new VCardPhoto();
linkedPhoto.setUri("https://example.com/portrait.jpg");
linkedPhoto.setPhotoType(VCardPhotoType.JPEG);
linkedPhoto.setValueLocation(VCardValueLocation.URL);
contact.getIdentificationInfo().setPhoto(linkedPhoto);

ป้ายกำหนดที่อยู่ที่จัดรูปแบบ

ป้ายกำกับคือข้อความที่จัดรูปแบบพร้อมพิมพ์สำหรับที่อยู่การจัดส่ง ป้ายกำกับอยู่ใน VCardLabelCollection เป็น VCardLabel รายการ แต่ละรายการมีแท็กประเภทที่อยู่

VCardContact contact = VCardContact.load("contact.vcf");

for (VCardLabel label : contact.getLabels()) {
    System.out.println(label.getAddressType());
    System.out.println(label.getAddress());
}

อ่านโมเดลกลับ

หลังจากโหลด vCard ชุดคุณสมบัติเช่นเดียวกันจะเปิดเผยข้อมูลที่แยกวิเคราะห์แล้ว

VCardContact loaded = VCardContact.load("contact.vcf");

if (loaded.getIdentificationInfo() != null) {
    System.out.println(loaded.getIdentificationInfo().getDisplayName());
}

for (VCardDeliveryAddress address : loaded.getDeliveryAddresses()) {
    System.out.println(address.getAddressType() + ": " + address.getStreet() + ", " + address.getLocality());
}

for (VCardTelephoneNumber phone : loaded.getTelephoneNumbers()) {
    System.out.println(phone.getTelephoneType() + ": " + phone.getTelephoneNumber());
}

if (loaded.getGeo() != null) {
    System.out.println("Geo: " + loaded.getGeo().getLatitude() + ", " + loaded.getGeo().getLongitude());
}

ความปลอดภัย (คีย์สาธารณะหรือใบรับรอง)

นี้ VCardSecurity คุณสมบัติเปิดเผยคีย์สาธารณะหรือใบรับรองการรับรองที่เก็บไว้ใน vCard (the KEY คุณสมบัติ). ค่าของมัน saveToPEM วิธีเขียนคีย์นั้นไปยังไฟล์ PEM

VCardContact signed = VCardContact.load("signed.vcf");

if (signed.getSecurity() != null && signed.getSecurity().getKey() != null) {
    System.out.println("Key type: " + signed.getSecurity().getType());

    // Export the embedded public key / certificate to a PEM file
    signed.getSecurity().saveToPEM("public-key.pem");
}

โหลด vCard

ใช้ static VCardContact.load วิธีอ่าน vCard จากไฟล์หรือสตรีม เมื่อโหลดเสร็จคุณสมบัติของข้อมูลติดต่อจะพร้อมใช้งานผ่านชุดคุณสมบัติเช่นเดียวกับที่ใช้ในการสร้าง

// Load from a file
VCardContact contact = VCardContact.load("contact.vcf");
System.out.println(contact.getIdentificationInfo().getDisplayName());

// Load from a stream
try (InputStream stream = Files.newInputStream(Paths.get("contact.vcf"))) {
    VCardContact fromStream = VCardContact.load(stream);
    System.out.println(fromStream.getIdentificationInfo().getDisplayName());
}

โหลดด้วยการเข้ารหัสเฉพาะ

เพื่อควบคุมการเข้ารหัสข้อความที่ใช้ขณะอ่าน ให้ส่งผ่าน VCardLoadOptions อ็อบเจ็กต์พร้อมกำหนดรหัสการเข้ารหัสที่ต้องการ

VCardLoadOptions loadOptions = new VCardLoadOptions();
loadOptions.setPreferredEncoding(StandardCharsets.UTF_8);

VCardContact contact = VCardContact.load("contact.vcf", loadOptions);

อ่านหลายข้อมูลติดต่อจากไฟล์เดียว

ไฟล์ vCard อาจมีข้อมูลติดต่อมากกว่าหนึ่งรายการ ใช้ static isMultiContacts วิธีเพื่อตรวจสอบว่าไฟล์หรือสตรีมมีหลายข้อมูลติดต่อ และ loadAsMultiple เพื่ออ่านทั้งหมดเข้าสู่ List<VCardContact>. ทั้งสองวิธีมีการโอเวอร์โหลดสำหรับไฟล์, สตรีม, และตัวเลือกการโหลด:

  • static boolean isMultiContacts(String filePath) / isMultiContacts(InputStream stream)
  • static List<VCardContact> loadAsMultiple(String filePath) / loadAsMultiple(String filePath, VCardLoadOptions options)
  • static List<VCardContact> loadAsMultiple(InputStream stream) / loadAsMultiple(InputStream stream, VCardLoadOptions options)
try (InputStream stream = Files.newInputStream(Paths.get("contacts.vcf"))) {
    if (VCardContact.isMultiContacts(stream)) {
        List<VCardContact> contacts = VCardContact.loadAsMultiple(stream);

        for (VCardContact contact : contacts) {
            System.out.println(contact.getIdentificationInfo().getDisplayName());
        }
    }
}

ตัวเลือกการบันทึก

นี้ VCardSaveOptions คลาสกำหนดวิธีการเขียนข้อมูลติดต่อ:

  • Version — เวอร์ชัน vCard ที่ส่งออกจาก VCardVersion enumeration: V21 (vCard 2.1, ค่าเริ่มต้น) V30 (3.0) หรือ V40 (4.0)
  • PreferredTextEncoding — การเข้ารหัสที่ใช้เมื่อเขียนไฟล์
  • UseExtensions — ว่าเป็นแบบขยาย (X-) สามารถเขียนคุณสมบัติได้ ค่าเริ่มต้นคือ true.
  • ProductId — ค่าที่เขียนลงใน PRODID คุณสมบัติ.
VCardContact contact = VCardContact.load("contact.vcf");

VCardSaveOptions saveOptions = new VCardSaveOptions();
saveOptions.setVersion(VCardVersion.V30);                    // write vCard 3.0 (the default is V21)
saveOptions.setPreferredTextEncoding(StandardCharsets.UTF_8); // encoding used when writing
saveOptions.setUseExtensions(true);                          // allow extended (X-) properties
saveOptions.setProductId("My Company");                      // PRODID value

contact.save("contact_v3.vcf", saveOptions);

เวอร์ชันสามารถส่งตรงไปยังคอนสตรัคเตอร์ได้เช่นกัน:

VCardContact contact = VCardContact.load("contact.vcf");
contact.save("contact_v4.vcf", new VCardSaveOptions(VCardVersion.V40));

ดูเพิ่มเติม