加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax上传文件和csv文件读写

发布时间:2020-12-16 01:39:21 所属栏目:百科 来源:网络整理
导读:解析csv文件工具类 public class CSVFileUtil { /** * 解析csv文件 到一个list中 每个单元个为一个String类型记录,每一行为一个list。 再将所有的行放到一个总list中 */ public static ListString readCSVFile(String path) throws IOException { InputStrea
解析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")
public void doGroupUserUpload(HttpServletRequest request,
HttpServletResponse response,
@RequestParam("uploadGroupUserfile") CommonsMultipartFile file) throws IOException {
// 写入文件
String fileName = "";
if(file.getSize()>0){
fileName = groupUserUploadUrl + DateUtil.getCurrentTimeSeconds()
+ new Random().nextInt(999999) + ".csv";
CSVFileUtil.writeCSVFile(fileName,file.getInputStream());
}

// 统计总数
int count = 0;
if(StringUtils.isNotBlank(fileName) && fileName.endsWith(".csv")){
List<String> dataList = CSVFileUtil.readCSVFile(fileName);
if (null != dataList) {
count = dataList.size();
}
}
response.setContentType("text/html;charset=UTF-8");
JSONObject json = new JSONObject();
json.put("fileName",fileName);
json.put("count",count);
response.getWriter().write(json.toString());
response.getWriter().flush();
response.getWriter().close();
}


前端层

<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"); } }); }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读