jsp上传.vcf文件来读取手机号码

时间:2014-12-09 12:51:29

标签: java jquery spring jsp filereader

如何在jsp中读取.vcf文件或如何将.vcf文件从html传递到控制器。 我正在使用spring mvc。我想阅读.vcf中的联系人。 我的jsp代码:

<span><input type="file" id="vcfFile" name="vcfFile" /> </span>

jquery代码:

$("#vcfUpload").click(function (){
                        var reader = new FileReader();
                        reader.onloadend = function(){
                         $
                            .ajax({
                                url : "../acquaintance/readingContactsFromVcfFile?vcfFileData="+reader,
                                type : "post",
                                contentType : "application/json; charset=utf-8",
                                cache : false,
                                dataType : "json",
                                success : function(data) {
                                alert(data.message);
                                }
                            });
                        };
                        reader.readAsText(document.getElementById('vcfFile').files[0]);
                    });

控制器代码:

@RequestMapping(value = { "/readingContactsFromVcfFile" }, method = RequestMethod.POST)
public @ResponseBody
ModelMap readContactsFromVcfFile(@RequestParam(value = "vcfFileData")Object vcfData) throws UserServiceException {
    log.info("vcf file reading"+ vcfData);
    ModelMap modelMap = new ModelMap();
    try {
        List<VCard> Vcards = Ezvcard.parseHtml((String) vcfData).all();
        for (VCard vcard : Vcards) {
            log.info("name"+vcard.getFormattedName().getValue());
            }
        modelMap.addAttribute("message","success");
    } catch (Exception IoExp) {
        log.info("exception while reading contacts from vcf file",IoExp);
        modelMap.addAttribute("message","failed");
    }
    return modelMap;
} 

我正在使用Vcard for external:https://code.google.com/p/ez-vcard/wiki/Examples#Example_1.1:_Reading_from_a_plain-text_vCard

0 个答案:

没有答案