文件上传

CSV文件的上传总结

1.首选定义上传文件的格式,我这里将文件的格式设置为UTF-8

2.文件名的取得

String fileName = file.getOriginalFilename();

3.文件的路径设定

String path = "C:/path/";

4.指定文件夹文件创建

File filePath= new File(path);

File file = new File(filePath, File.separator + fileName);

 

5.文件不存在,则创建

if (!file.exists()) {
file.mkdirs();
}

6.CSV文件读取

String fileUrl = dest.toString();//文件字符串转换

List<List<String>> records = readCsvFile(file);

7.

public List<List<String>> readCsvFile(String file) {

List<List<String>> readData = new ArrayList<List<String>>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
String rowData = null;
while ((rowData= reader.readLine()) != null) {
if (!"".equals(rowData)) {
String[] record = rowData.trim().substring(1, rowData.trim().length() - 1).replace("\"", "").split(",", -1);
readData.add(Arrays.asList(record));
}
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
return rowData ;
}

posted @ 2020-09-08 15:45  一颗种子,一粒沙  阅读(136)  评论(0编辑  收藏  举报