Làm việc với Các Tệp vCard (VCF) trong Java
Bài viết này bao gồm VCardContact lớp, đọc và ghi các tệp vCard (VCF) độc lập với Outlook và MAPI. Để tạo và quản lý Outlook MapiContact các mục — có thể cũng được xuất sang VCF — xem Làm việc với Danh bạ Outlook.
VCardContact vs MapiContact
Aspose.Email cung cấp danh bạ qua hai lớp khác nhau, và việc chọn đúng lớp sẽ tránh nhiều nhầm lẫn:
- VCardContact — một đối tượng vCard thuần. Sử dụng khi nguồn và đích của bạn là các tệp vCard (VCF) và bạn không cần tính năng Outlook/MAPI. Nó đọc và ghi VCF trực tiếp, hỗ trợ vCard 2.1/3.0/4.0 và nhiều liên hệ trong một tệp.
- MapiContact — một liên hệ Outlook. Sử dụng khi bạn làm việc với MSG/PST hoặc cần các thuộc tính đặc thù của MAPI.
MapiContactcũng có thể nhập và xuất sang VCF quafromVCardvàsavephương thức (xem Làm việc với Danh bạ Outlook).
Phần còn lại của bài viết này tập trung vào VCardContact.
Tạo một vCard và Lưu Nó
Cái VCardContact lớp có một constructor không tham số và tiết lộ các tập hợp thuộc tính kiểu mạnh như VCardIdentificationInfo, VCardOrganization, VCardEmailCollection, VCardTelephoneNumberCollection, và VCardDeliveryAddressCollection.
Đoạn mã sau cho bạn thấy cách xây dựng một liên hệ từ đầu và lưu nó như một tệp 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");
IdentificationInfo.FullName là một chuỗi, trong Aspose.Email cho Java nó là một VCardFullName đối tượng với các phần họ/tên đệm/tên riêng biệt.
Mô Hình Thuộc Tính Liên Hệ
Ngoài tên và email đã hiển thị ở trên, một VCardContact tiết lộ toàn bộ mô hình dữ liệu vCard thông qua các tập hợp thuộc tính kiểu mạnh. Các đoạn mã dưới mở rộng contact được tạo trước đó.
Địa Chỉ Bưu Điện
Địa chỉ bưu điện tồn tại trong VCardDeliveryAddressCollection như VCardDeliveryAddress mục. Kiểu địa chỉ nhận một sự kết hợp của VCardDeliveryAddressType cờ (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);
Số Điện Thoại
Số điện thoại tồn tại trong VCardTelephoneNumberCollection như VCardTelephoneNumber các mục. Kiểu điện thoại nhận một tổ hợp của VCardTelephoneType cờ (VOICE, WORK, HOME, CELL, FAX, PAGER, PREF, và hơn thế nữa).
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);
Vị Trí Địa Lý
Cái Geo thuộc tính lưu trữ vị trí toàn cầu dưới dạng float vĩ độ và kinh độ.
VCardContact contact = new VCardContact();
VCardGeo geo = new VCardGeo();
geo.setLatitude(48.858844f);
geo.setLongitude(2.294351f);
contact.setGeo(geo);
Ảnh
Ảnh liên hệ được đặt trên IdentificationInfo.Photo Như một VCardPhoto. Nó có thể được nhúng trực tiếp từ byte ảnh hoặc tham chiếu bằng URL. Định dạng ảnh được xác định bởi VCardPhotoType và chế độ lưu trữ bằng 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);
Nhãn Địa Chỉ Định Dạng
Nhãn là văn bản đã định dạng, sẵn sàng in của một địa chỉ giao hàng. Nhãn tồn tại trong VCardLabelCollection như VCardLabel các mục, mỗi mục được gắn thẻ với một loại địa chỉ.
VCardContact contact = VCardContact.load("contact.vcf");
for (VCardLabel label : contact.getLabels()) {
System.out.println(label.getAddressType());
System.out.println(label.getAddress());
}
VCardLabel không có constructor công khai, vì vậy các nhãn được đọc từ một vCard hiện có thay vì được tạo trong mã. Để ghi một nhãn vào tệp bạn tự viết, điền vào VCardDeliveryAddress trường thay vào đó.
Đọc lại Mô Hình
Sau khi tải vCard, cùng các tập hợp thuộc tính sẽ tiết lộ dữ liệu đã phân tích.
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());
}
Bảo Mật (Khóa Công Khai hoặc Chứng Chỉ)
Cái VCardSecurity tiết lộ khóa công khai hoặc chứng chỉ xác thực được lưu trong vCard (the KEY thuộc tính). Thuộc tính saveToPEM phương thức ghi khóa đó vào tệp 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");
}
Tải một vCard
Sử dụng tĩnh VCardContact.load phương thức để đọc vCard từ tệp hoặc luồng. Khi đã tải, các thuộc tính của danh bạ có sẵn qua cùng các tập hợp thuộc tính đã dùng khi tạo nó.
// 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());
}
Tải với Mã Hoá Cụ Thể
Để kiểm soát mã hoá văn bản được sử dụng khi đọc, truyền một VCardLoadOptions đối tượng với mã hóa ưa thích đã được đặt.
VCardLoadOptions loadOptions = new VCardLoadOptions();
loadOptions.setPreferredEncoding(StandardCharsets.UTF_8);
VCardContact contact = VCardContact.load("contact.vcf", loadOptions);
Đọc Nhiều Danh Bạ Từ Một Tệp Đơn
Một tệp vCard có thể chứa hơn một danh bạ. Sử dụng phương thức tĩnh isMultiContacts phương thức để kiểm tra xem một tệp hoặc luồng có chứa nhiều danh bạ hay không, và loadAsMultiple để đọc chúng tất cả vào một List<VCardContact>. Cả hai phương pháp đều có các overload cho tệp, luồng và tùy chọn tải:
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());
}
}
}
isMultiContacts(InputStream) đọc luồng để phát hiện số lượng liên hệ. Mở lại luồng (hoặc sử dụng các overload theo đường dẫn tệp) trước khi gọi loadAsMultiple trên cùng nguồn.
Tùy chọn Lưu
Cái VCardSaveOptions lớp tùy chỉnh cách một danh bạ được ghi:
Version— phiên bản vCard đầu ra từ VCardVersion enumeration:V21(vCard 2.1, mặc định),V30(3.0), hoặcV40(4.0).PreferredTextEncoding— cách mã hoá được sử dụng khi ghi tệp.UseExtensions— liệu mở rộng (X-) các thuộc tính có thể được viết. Mặc định làtrue.ProductId— giá trị được ghi vàoPRODIDthuộc tính.
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);
Phiên bản cũng có thể được truyền trực tiếp vào constructor:
VCardContact contact = VCardContact.load("contact.vcf");
contact.save("contact_v4.vcf", new VCardSaveOptions(VCardVersion.V40));
Xem Thêm
- Làm việc với Danh bạ Outlook — tạo, lưu và đọc Outlook
MapiContactcác mục, bao gồm nhập và xuất VCF. - Làm việc với Liên hệ trong Tệp PST — lưu và đọc danh bạ trong PST.