ajax上传文件和csv文件读写
来源:互联网 发布:nginx 显示目录 编辑:程序博客网 时间:2024/06/14 05:29
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");
}
});
}
- ajax上传文件和csv文件读写
- commons-csv读写CSV文件
- PHP 读写Excel 和 CSV 文件
- 读写csv 文件
- Java读写CSV文件
- csv文件读写操作
- Java读写CSV文件
- csv文件读写
- Python读写csv文件
- CSV文件的读写
- C++读写CSV文件
- 读写.csv文件
- C++读写CSV文件
- python 读写 csv文件
- C++读写CSV文件
- 读写csv文件
- Python读写csv文件
- Python读写csv文件
- String和StringBuilder的区别
- 单链表的反转(非递归)
- osg粒子
- 相机、分类器、双目、结构光、打印机分辨率的基本知识
- ALAsset,ALAssetsLibrary,ALAssetsgroup常见属性及用法
- ajax上传文件和csv文件读写
- 一个10年的程序员的感叹!
- Jenkins权限设计错误解决办法
- 魅族系列手机无法连接eclipse解决办法
- ReactOS实现的兼容NT内核分析--KfLowerIrql函数
- String——第一个只出现一次的字符
- POJ 2159 Going Home 网络流 最小费用最大流
- 视图
- 模式识别入门学习