SpringBoot上传文件

以上传excel为例

1、pom

<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
          <version>3.14</version>
</dependency>
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
          <version>3.14</version>
</dependency>

2、代码

package net.wecash.oms.controller;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author:Luobh
 * @create: 2019-10-08
 **/
@RestController
@RequestMapping("/dataImport")
public class DataImportController {

    @PostMapping(value = "/uploadFile")
    public Integer uploadFile(@RequestParam Integer id,@RequestParam("templateFile") MultipartFile multipartFile ,@RequestParam("templateFile2") MultipartFile multipartFile2) {
        try {
            System.out.println("file1");
            XSSFWorkbook xwb = new XSSFWorkbook(multipartFile.getInputStream());
            XSSFSheet sheet = xwb.getSheetAt(0);
            for (int i = 0; i < sheet.getPhysicalNumberOfRows() ; i++) {
                if (sheet.getRow(i) != null && sheet.getRow(i).getCell(0) != null) {
                    System.out.println(sheet.getRow(i).getCell(0));
                }
            }
            System.out.println(id);

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (multipartFile2 != null ) {
            System.out.println("file2");
            try {
                XSSFWorkbook xwb = new XSSFWorkbook(multipartFile2.getInputStream());
                XSSFSheet sheet = xwb.getSheetAt(0);
                for (int i = 0; i < sheet.getPhysicalNumberOfRows() ; i++) {
                    if (sheet.getRow(i) != null && sheet.getRow(i).getCell(1) != null) {
                        System.out.println(sheet.getRow(i).getCell(1));
                    }
                }
                System.out.println(id);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return id;
    }

}

3、postman测试

SpringBoot上传文件

4、控制台输出:

file1
模板参数填写规范:参数填写规则:${参数编码};
编号
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
123
file2

编码
nowdate
year
month
day
channelName1
name
idCard
phone
bankCard
bankName
address
quotaNum
quota
totalNum
total
stage1
ordereveryNum
orderevery
applyeveryNum
applyevery
goodsName
serviceAmount1
serviceAmount2
serviceType
serviceAmountA
everyServiceAmountB
serviceAmountB
rate
stage2
deliverTime
deliveryear
delivermonth
paymentDay1
loanTime
loanyear
loanmonth
paymentDay2
orderID
uid
email
channelmobile
channelemail
123