zoukankan      html  css  js  c++  java
  • 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";

                     }

            }

    不经一番彻骨寒,哪有梅花扑鼻香?
  • 相关阅读:
    函数
    python操作文件
    POJ-2689-Prime Distance(素数区间筛法)
    POJ-2891-Strange Way to Express Integers(线性同余方程组)
    POJ-2142-The Balance
    POJ-1061-青蛙的约会(扩展欧几里得)
    Educational Codeforces Round 75 (Rated for Div. 2) D. Salary Changing
    Educational Codeforces Round 75 (Rated for Div. 2) C. Minimize The Integer
    Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes
    Educational Codeforces Round 75 (Rated for Div. 2) A. Broken Keyboard
  • 原文地址:https://www.cnblogs.com/zongyao/p/13831210.html
Copyright © 2011-2022 走看看