العمل مع ملفات vCard (VCF) في جافا
تغطي هذه المقالة 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 الصف يحتوي على مُنشئ بدون معلمات ويكشف عن مجموعات خصائص ذات نوع قوي مثل 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");
IdentificationInfo.FullName هو سلسلة، في Aspose.Email لجافا هو VCardFullName كائن بأجزاء الاسم الأول/الوسطى/العائلة منفصلة.
نموذج خاصية جهة الاتصال
إلى جانب الاسم والبريد الإلكتروني المعروضين أعلاه، VCardContact يكشف عن نموذج بيانات vCard الكامل عبر مجموعات خصائص ذات نوع قوي. يمدد المقتطفات أدناه 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);
صورة
يتم تعيين صورة جهة الاتصال على 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());
}
VCardLabel ليس له منشئ عام، لذا يتم قراءة التسميات من بطاقة vCard موجودة بدلاً من تكوينها في الكود. لإصدار تسمية في ملف تكتبه بنفسك، املأ الـ VCardDeliveryAddress الحقول بدلاً من ذلك.
قراءة النموذج مرة أخرى
بعد تحميل 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 (ال KEY property). خاصيته 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
استخدم الثابت 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 على أكثر من جهة اتصال واحدة. استخدم الثابت 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());
}
}
}
isMultiContacts(InputStream) يقرأ التدفق لاكتشاف عدد جهات الاتصال. أعد فتح التدفق (أو استخدم تحميلات مسار الملف) قبل الاستدعاء loadAsMultiple على نفس المصدر.
خيارات الحفظ
الـ VCardSaveOptions الصف يُخصص كيفية كتابة جهة الاتصال:
Version— إصدار vCard الناتج من VCardVersion تعداد: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));
انظر أيضًا
- التعامل مع جهات اتصال Outlook — إنشاء، حفظ، وقراءة Outlook
MapiContactالعناصر، بما في ذلك استيراد وتصدير VCF. - العمل مع جهات الاتصال في ملف PST — تخزين وقراءة جهات الاتصال داخل PST.