SpringBoot中关于上传文件

不多说,上代码
首先是在Controller
Controller层图片

	@Autowired
    private ImportService importService; 
    
@RequestMapping("/cust_balance_import_todo")
    @ResponseBody
    public String cust_balance_import_todo( @RequestParam("userInfo") MultipartFile file) throws Exception {
        if(file.isEmpty()){//其中这个file自己获取的,别全部都copy完啊
            return "false";
        }
        String fileName = file.getOriginalFilename();//获取文件夹名字
        if(fileName.indexOf("xls")<0){
            return "上传文件类型不符合要求,请确定是 (.xls/.xlsx)后缀的Excel 文件";
        }
        //List<List<Object>> list = ReadExcel.readExcel(file);
        InputStream inputStream = file.getInputStream();
        //这里是重要的
        List<List<Object>> list = importService.getBankListByExcel(inputStream, file.getOriginalFilename());
        //这里获取完,根据需要写你的东东 这个list
        inputStream.close();
        return null;
    }

importService类

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class ImportService {


    /**
     * 处理上传的文件
     *
     * @param in
     * @param fileName
     * @return
     * @throws Exception
     */
    public List getBankListByExcel(InputStream in, String fileName) throws Exception {
        List list = new ArrayList<>();
        //创建Excel工作薄
        Workbook work = this.getWorkbook(in, fileName);
        if (null == work) {
            throw new Exception("创建Excel工作薄为空!");
        }
        Sheet sheet = null;
        Row row = null;
        Cell cell = null;

        for (int i = 0; i < work.getNumberOfSheets(); i++) {
            sheet = work.getSheetAt(i);
            if (sheet == null) {
                continue;
            }

            for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
                row = sheet.getRow(j);
                if (row == null || row.getFirstCellNum() == j) {
                    continue;
                }

                List<Object> li = new ArrayList<>();
                for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
                    cell = row.getCell(y);
                    li.add(cell);
                }
                list.add(li);
            }
        }
        work.close();
        return list;
    }

    /**
     * 判断文件格式
     *
     * @param inStr
     * @param fileName
     * @return
     * @throws Exception
     */
    public Workbook getWorkbook(InputStream inStr, String fileName) throws Exception {
        Workbook workbook = null;
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        if (".xls".equals(fileType)) {
            workbook = new HSSFWorkbook(inStr);
        } else if (".xlsx".equals(fileType)) {
            workbook = new XSSFWorkbook(inStr);
        } else {
            throw new Exception("请上传excel文件!");
        }
        return workbook;
    }

}

如果发现博文中有错误,还请各位老鸟多多指点指点

猜你喜欢

转载自blog.csdn.net/AdminPwd/article/details/92792326