zoukankan      html  css  js  c++  java
  • 上传入库整个过程

    package com.irm.jd.service;
    
    import com.irm.jd.entity.Sit;
    import com.irm.jd.mapper.SitMapper;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.UUID;
    
    @Service
    public class SitService {
        @Autowired
        private SitMapper sitMapper;
    
        @Async
        public void upload(MultipartFile file) throws IOException {
            // 执行文件上传
            String originalFilename = file.getOriginalFilename();
            String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
            UUID uuid = UUID.randomUUID();
            String fileName = uuid + fileExtension;
            String dir = "D:/uploads/" + fileName;
            File willUploadFile = new File(dir);
            file.transferTo(willUploadFile);
            try {
                FileInputStream fileInputStream = new FileInputStream(willUploadFile);
                XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fileInputStream);
                if (xssfWorkbook != null) {
                    XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
                    List<Sit> sits = new ArrayList<>();
                    int count = 0;
                    for (int i = 0; i < sheet.getLastRowNum(); i++) {
                        XSSFRow row = sheet.getRow(i);
                        if (row != null) {
                            Sit sit = new Sit();
                            for (int k = 0; k < row.getLastCellNum(); k++) {
                                if (row.getCell(0) != null) {
                                    if (!row.getCell(0).toString().equals("null") && !row.getCell(0).toString().equals("")) {
                                        sit.setGongwei(row.getCell(0).toString());
                                    }
                                }
    
                                if (row.getCell(1) != null) {
                                    if (!row.getCell(1).toString().equals("null") && !row.getCell(1).toString().equals("")) {
                                        sit.setFloor(row.getCell(1).toString());
                                    }
                                }
    
                                if (row.getCell(2) != null) {
                                    if (!row.getCell(2).toString().equals("null") && !row.getCell(2).toString().equals("")) {
                                        sit.setTelephone_ip(row.getCell(2).toString());
                                    }
                                }
    
    
                                if (row.getCell(3) != null) {
                                    if (!row.getCell(3).toString().equals("null") && !row.getCell(3).toString().equals("")) {
                                        sit.setComputer_ip(row.getCell(3).toString());
                                    }
                                }
    
    
                                if (row.getCell(4) != null) {
                                    if (!row.getCell(4).toString().equals("null") && !row.getCell(4).toString().equals("")) {
                                        sit.setArea(row.getCell(4).toString());
                                    }
                                }
                            }
                            sits.add(sit);
                        } else {
                            System.out.println("null");
                        }
                    }
                    sitMapper.insertSite(sits);
                }
            } catch (IOException e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
    }
    

      

  • 相关阅读:
    spring 学习-bean创建bean
    [转]spring 学习-bean创建-循环依赖-3
    [转]spring 学习-bean创建-循环依赖-2
    [转]spring 学习-bean创建-循环依赖-1
    2、python2.7,python3.9 ;在不同版本上获得整数类型运行的结果
    记一次Linux bash 命令行卡顿排查之警惕LD_PRELOAD环境变量
    CPU中断数查看与网卡中断绑核
    中华人民共和国 个人信息保护法 (全文学习)八章 七十四条 2021年11月1日起施行
    【模拟试题】旅行
    Vagrant出现的问题整理
  • 原文地址:https://www.cnblogs.com/leigepython/p/10191894.html
Copyright © 2011-2022 走看看