ajax上传文件和csv文件读写

来源:互联网 发布:nginx 显示目录 编辑:程序博客网 时间:2024/06/14 05:29
解析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");
       }
});
}


0 0