ajax上传文件和csv文件读写
解析csv文件工具类
public class CSVFileUtil { /** * 解析csv文件 到一个list中 每个单元个为一个String类型记录,每一行为一个list。 再将所有的行放到一个总list中 */ public static List<String> readCSVFile(String path) throws IOException { InputStreamReader fr = new InputStreamReader(new FileInputStream(path)); BufferedReader br = new BufferedReader(fr); String rec = null;// 一行 //String str;// 一个单元格 List<String> listFile = new ArrayList<String>(); try { // 读取一行,放过标题 rec = br.readLine(); while ((rec = br.readLine()) != null) { /*Pattern pCells = Pattern .compile("("[^"]*("{2})*[^"]*")*[^,]*,"); Matcher mCells = pCells.matcher(rec); List<String> cells = new ArrayList<String>();// 每行记录一个list // 读取每个单元格 while (mCells.find()) { str = mCells.group(); str = str.replaceAll( "(?sm)"?([^"]*("{2})*[^"]*)"?.*,","$1"); str = str.replaceAll("(?sm)("("))","$2"); cells.add(str); }*/ listFile.add(rec); } } catch (Exception e) { e.printStackTrace(); } finally { if (fr != null) { fr.close(); } if (br != null) { br.close(); } } return listFile; } /** * 解析csv文件 到一个list中 每个单元个为一个String类型记录,每一行为一个list。 再将所有的行放到一个总list中 */ public static void writeCSVFile(String filePath,InputStream is) throws IOException { FileOutputStream fos = null; try { fos = new FileOutputStream(filePath); byte[] b = new byte[1024]; while ((is.read(b)) != -1) { fos.write(b); } } catch (IOException e) { e.printStackTrace(); } finally { if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != fos) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) throws FileNotFoundException { InputStream is = new FileInputStream("D:template.csv"); FileOutputStream fos = new FileOutputStream("D:c.csv"); try { byte[] b = new byte[1024]; while ((is.read(b)) != -1) { fos.write(b); } is.close(); fos.close(); System.out.println(CSVFileUtil.readCSVFile("D:c.csv").size()); } catch (IOException e) { } finally { } } }
controller层 @RequestMapping(value = "/doGroupUserUpload.do")
前端层 <script src="/crm/js/jquery-plugin/ajaxfileupload.js" charset="UTF-8" type="text/javascript"></script>
/*文件上传*/ function doGroupUserUpload() { $.ajaxFileUpload({ url:contextPath+'/UserGroupController/doGroupUserUpload.do',secureuri: false,fileElementId: 'uploadGroupUserfile',dataType: 'json',success: function (data) { $("#userGruopEditForm #uploadFileUrl").val(data.fileName); $("#userGruopEditForm #sumNum").val(data.count); },error: function (data) { alert("error"); } }); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |