• 上传入库整个过程


    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();
            }
        }
    }
    

      

  • 相关阅读:
    字符数组数据映射转换到实体对象model注解方式 demo
    字符数组转换及数字求和 java8 lambda表达式 demo
    java8 Lambda及Stream学习笔记
    java读取txt文件行的两种方式对比
    sftp jsch文件移动备份的思路
    APOI2009-抢掠计划
    NOIP2011
    省选算法(转)
    割点
    实验十 团队作业6:团队项目用户验收&Beta冲刺
  • 原文地址:https://www.cnblogs.com/leigepython/p/10191894.html
Copyright © 2020-2023  润新知