Thursday, June 10, 2010

read .vcf files in java

this is sample coding.

getting contact details from .vcf files using java..

if you need emailid and firstname from contacts.vcf.

the below java program is used..

sample jsp



html:file size="25" property="contactFile" styleId="contactFile" name="contactForm" onchange="javascript:checkFileType(this.value)"




javascript check the only.vcf format
--------------------------------------

function checkFileType(fName){
var isValid=true;
fullName = fName;
shortName = fullName.match(/[^\/\\]+$/);
splitName = fullName.split(".");
fileType = splitName[1];
fileType = fileType.toLowerCase();

if (fileType == 'vcf' )
{

document.contactForm.action="address.do";
document.contactForm.submit();

}
else {
alert("File formats must be .vcf ");
isValid=false;

}

}



action class
------------------
ContactForm contact=(ContactForm)form;
FormFile myFile=contact.getContactFile();
String fileType=null;


//HttpSession session = request.getSession(true);
//Register reg=(Register)session.getAttribute("userbean");

if(myFile.getFileSize() != 0 || myFile.getFileName().equals(""))
{
try
{
String fileName = myFile.getFileName();
fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
if(fileType.equals("vcf")){


InputStream fis = myFile.getInputStream();
byte buf[] = new byte[2048];
int n = 0;

String newFile = "contact.vcf";
if(!fileName.equals(""))
{
// FileOutputStream fos = new FileOutputStream((new StringBuilder("C:/Users/CIM Services 4/Desktop/folder/2sellback/")).append(newFile).toString());
FileOutputStream fos = new FileOutputStream((new StringBuilder("/home/resum0/public_html/importfile/")).append(newFile).toString());
while((n = fis.read(buf)) != -1)
{
fos.write(buf);
}
fis.close();
fos.flush();
fos.close();
}


the above code is upload



File f = new File("/home/resum0/public_html/importfile/contact.vcf");
BufferedReader input = new BufferedReader(new FileReader(f));
StringBuffer mail = new StringBuffer();
StringBuffer fname = new StringBuffer();
while((line = input.readLine()) != null)
{
Address address = new Address();
Contact fAddress = new Contact();
StringTokenizer st = new StringTokenizer(line, ":");
int i = 1;
int k = 1;
String check = "no";
while(st.hasMoreTokens())
{
if(i % 2 == 0)
{
if(check.equals("mail"))
{
address.setEmailId(st.nextToken());
check = "no";
}
if(check.equals("fname"))
{
String name = st.nextToken();
if(name.equals("null"))
{
fAddress.setfName("no");
} else
{
fAddress.setfName(name);
}
check = "no";
}
} else
{
String str = st.nextToken();
if(str.equals("EMAIL;TYPE=INTERNET"))
{
check = "mail";
}
if(str.equals("FN"))
{
check = "fname";
}
}
i++;
}
if(fAddress.getfName() != null)
{
fNameList.add(fAddress);
}
if(address.getEmailId() != null)
{
contacList.add(address);
}
}
System.out.println(mail.toString());
System.out.println(fname.toString());
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
contact.setContactList(contacList);
contact.setfNameList(fNameList);
return mapping.findForward("success");
}

No comments:

Post a Comment