批量上传:
DbToExcel.Java
/**
* 课程上传
*/
public List<Course> excelToDb1(String excelpath) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
Workbook workbook = null;
Sheet sheet = null;
//conn = JdbcPoolUtils.getConnection();
conn=null;
List<Course> list=new ArrayList<Course>();
workbook = Workbook.getWorkbook(new File(excelpath));
sheet = workbook.getSheet(0);
int r = sheet.getRows();
for (int i = 1; i < r; i++) {
Course course=new Course();
course.setId(Integer.valueOf(sheet.getCell(1, i).getContents()));
course.setName(sheet.getCell(2, i).getContents());
course.setHour(Integer.valueOf(sheet.getCell(3, i).getContents()));
list.add(course);
}
workbook.close();
return list;
}
Dao层:
服务层:
控制层:
/**
* 文件上传
*/
@RequestMapping("/toFileUpload1.action")
public String toFileUpload1(Model model) {
String forword="admin/fileUploadCourse";
return forword;
}
@RequestMapping("/courseFileUpload.action")
public String courseFileUpload(@RequestParam("uploadfile") List<MultipartFile> uploadfile,
HttpServletRequest request) {
// 判断所上传文件是否存在
if (!uploadfile.isEmpty() && uploadfile.size() > 0) {
//循环输出上传的文件
for (MultipartFile file : uploadfile) {
// 获取上传文件的原始名称
String originalFilename = file.getOriginalFilename();
String dirPath = "D:/test1/";
File filePath = new File(dirPath);
System.out.println("dirPath:"+dirPath);
// 如果保存文件的地址不存在,就先创建目录
if (!filePath.exists()) {
filePath.mkdirs();
}
// 使用UUID重新命名上传的文件名称(上传人_uuid_原始文件名称)
String newFilename = "_"+UUID.randomUUID() +
"_"+originalFilename;
try {
// 使用MultipartFile接口的方法完成文件上传到指定位置
file.transferTo(new File(dirPath + newFilename));
List<Course> courses=dbToExcel1.excelToDb1(dirPath + newFilename);
courseService.addCourses(courses);
} catch (Exception e) {
e.printStackTrace();
return "jsp/error";
}
}
// 跳转到成功页面
return "jsp/success";
}else{
return"jsp/error";
}
}
批量上传:
DbToExcel.Java
/**
* 课程上传
*/
public List<Course> excelToDb1(String excelpath) throws Exception {
Connection conn = null;
PreparedStatement ps = null;
Workbook workbook = null;
Sheet sheet = null;
//conn = JdbcPoolUtils.getConnection();
conn=null;
List<Course> list=new ArrayList<Course>();
workbook = Workbook.getWorkbook(new File(excelpath));
sheet = workbook.getSheet(0);
int r = sheet.getRows();
for (int i = 1; i < r; i++) {
Course course=new Course();
course.setId(Integer.valueOf(sheet.getCell(1, i).getContents()));
course.setName(sheet.getCell(2, i).getContents());
course.setHour(Integer.valueOf(sheet.getCell(3, i).getContents()));
list.add(course);
}
workbook.close();
return list;
}
Dao层:
服务层:
控制层:
/**
* 文件上传
*/
@RequestMapping("/toFileUpload1.action")
public String toFileUpload1(Model model) {
String forword="admin/fileUploadCourse";
return forword;
}
@RequestMapping("/courseFileUpload.action")
public String courseFileUpload(@RequestParam("uploadfile") List<MultipartFile> uploadfile,
HttpServletRequest request) {
// 判断所上传文件是否存在
if (!uploadfile.isEmpty() && uploadfile.size() > 0) {
//循环输出上传的文件
for (MultipartFile file : uploadfile) {
// 获取上传文件的原始名称
String originalFilename = file.getOriginalFilename();
String dirPath = "D:/test1/";
File filePath = new File(dirPath);
System.out.println("dirPath:"+dirPath);
// 如果保存文件的地址不存在,就先创建目录
if (!filePath.exists()) {
filePath.mkdirs();
}
// 使用UUID重新命名上传的文件名称(上传人_uuid_原始文件名称)
String newFilename = "_"+UUID.randomUUID() +
"_"+originalFilename;
try {
// 使用MultipartFile接口的方法完成文件上传到指定位置
file.transferTo(new File(dirPath + newFilename));
List<Course> courses=dbToExcel1.excelToDb1(dirPath + newFilename);
courseService.addCourses(courses);
} catch (Exception e) {
e.printStackTrace();
return "jsp/error";
}
}
// 跳转到成功页面
return "jsp/success";
}else{
return"jsp/error";
}
}