• SSM框架批量上传


    以课为例,有3个字段,id,hour,name

    /**

        * 批量上传

        */

            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";

                     }

            }

    不经一番彻骨寒,哪有梅花扑鼻香?
  • 相关阅读:
    在同一台机器上让Microsoft SQL Server 2000/ SQL2005/ SQL2008共存
    HTML 中<style>中</style>里面<!-- -->标签是干嘛的
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    怎样清除td和input之间空隙
    aspx页面中, <%= % > 与 <%# % > 的区别
    python之day13(ORM,paramiko模块,堡垒机)
    python之day12(线程池,redis,rabbitMQ)
    RabbitMQ与SQLAlchemy(预习)
    redis操作篇
    python之day11(线程,进程,协程,memcache,redis)
  • 原文地址:https://www.cnblogs.com/zongyao/p/13831210.html
Copyright © 2020-2023  润新知