【发布时间】:2016-07-17 15:39:30
【问题描述】:
我有数千条记录存储在 Excel 表中,我需要将这些记录上传到数据库中,目前我正在使用 Spring 控制器类进行上传,在我的类中我使用简单的 BufferedOutputStream 和 FileReader 类,所以我的要求是我需要在将数据上传到数据库时显示一个包含百分比的 Jquery 进度条。
我的示例代码。
String rootPath = request.getSession().getServletContext().getRealPath("/");
File dir = new File(rootPath + File.separator + "uploadedfile");
if (!dir.exists()) {
dir.mkdirs();
}
File serverFile = new File(dir.getAbsolutePath() + File.separator + form.getEmpFile().getOriginalFilename());
try {
try (InputStream is = form.getEmpFile().getInputStream();
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile))) {
int i;
//write file to server
while ((i = is.read()) != -1) {
stream.write(i);
}
stream.flush();
}
}
catch (IOException e) {
model.addAttribute("msg", "failed to process file because : " + e.getMessage());
}
String[] nextLine;
try (FileReader fileReader = new FileReader(serverFile); CSVReader reader = new CSVReader(fileReader, ';', '\'', 1);) {
while ((nextLine = reader.readNext()) != null) {
for (int i = 0; i < nextLine.length; i++) {
nextLine[i] = nextLine[i].trim();
if (!nextLine[i].equals("")) {
String[] data = nextLine[i].split(",");
【问题讨论】:
标签: javascript java jquery spring-mvc