Flex TXT文件导入


  在上一篇文章中,我们做了一个文件上传、导入组件,其实就是一个文件选择并上传的组件,该组件是基于Blazeds来实现的。本文利用该组件实现Flex客户端Txt文件的导入,并将导入的txt文本数据展现到Flex客户端的AdvancedDataGrid中,其它文件格式如excel等导入同理。
  假设Txt文件内容如下,该文件没有抬头,分别表示用户ID、用户名称、用户IP。     上文中提到的组件中其文件导入方法为public function importFile(headers:Array):Boolean,该方法调用的是blazeds 服务的public Object[] importFile(byte[] content, List header, String fileType)方法,为此需要实现该方法。该方法核心就是txt文件的生成,txt文件的解析,代码如下:     public Object[] importFile(byte[] content, List header, String fileType)throws Exception{        Object[] result = new Object[0];        String pathname = "/usr/temp/temp01.txt";        //write file        FileOutputStream stream = new FileOutputStream(file);        stream.write(content);        stream.close();        //read file        result = parseTXT(header, pathname);        file.delete();        return result;     }     privatestatic Object[] parseTXT(List header, String filepath){        try{            FileReader reader = new FileReader(filepath);            BufferedReader br = new BufferedReader(reader);            String content = null;            Map map = newHashMap();            List dataList = newArrayList();            while((content = br.readLine()) != null) {               String[] values = content.split("\t");               map = newHashMap();               for(int i=0; i=values.length){                      map.put(header.get(i), null);                   }else{                      map.put(header.get(i), values[i]);                   }               }               dataList.add(map);            }            br.close();            reader.close();            return dataList.toArray();        }catch(Exception e){            thrownew AppException(e);        }     } 客户端建立一个Flex Application,关键是使用文件导入组件,并将导入结果展示到表格中显示,代码如下:  flex/spark"                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:services="services.*"                width="561" height="292" creationComplete="init()" xmlns:fileupload="fileupload.*">                                 
  
  
  
  
  
  
  
  width="537" height="207">
  
  
  
  
  
  
  

你可能感兴趣的:(Flex)