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

      

  • 相关阅读:
    javascript里面this机制的几个例子
    把List<string>集合,编程string,并以“,”号分割
    比较集合List<T>集合,前后多了哪些数据,少了哪些数据Except
    c# Web.config中 windows连接数据库
    MVC之图片验证码
    匿名函数-简单实例
    c# 如何找到项目中图片的相对路径
    MVC下 把数据库中的byte[]值保存成图片,并显示在view页面
    MVC下form表单一次上传多种类型的图片(每种类型的图片可以上传多张)
    关于Visual Studio未能加载各种Package包的解决
  • 原文地址:https://www.cnblogs.com/leigepython/p/10191894.html
Copyright © 2011-2022 走看看