栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

解决JAVA一个接口同时接收【文件】和【json】2种参数,并完成业务实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解决JAVA一个接口同时接收【文件】和【json】2种参数,并完成业务实现

需求: 现在需要一个接口同时接收文件(excel表格)和json参数 代码 前端使用formdata进行传输 后端代码

maven依赖


   org.apache.poi
    poi
    4.1.2


    org.apache.poi
    poi-ooxml
    4.1.2

ExcelUtils工具类,解析表格数据,不解析表头行

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

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


public class ExcelUtils {

    public static List> getListByExcel(InputStream in, String fileName) throws Exception {
        List> list = new ArrayList<>();

        // 创建excel工作簿
        Workbook work = getWorkbook(in, fileName);
        if (null == work) {
            throw new Exception("创建Excel工作薄为空!");
        }
        Sheet sheet = null;
        Row row = null;
        Cell cell = null;

        sheet = work.getSheetAt(0);
        // 滤过第一行标题
        for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
            row = sheet.getRow(j);
            if (row == null || row.getFirstCellNum() == j) {
                continue;
            }
            List 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;
    }

    private static Workbook getWorkbook(InputStream in, String fileName) throws Exception {
        Workbook book = null;
        String filetype = fileName.substring(fileName.lastIndexOf("."));

        if (".xls".equals(filetype)) {
            book = new HSSFWorkbook(in);
        } else if (".xlsx".equals(filetype)) {
            book = new XSSFWorkbook(in);
        } else {
            throw new Exception("请上传excel文件!");
        }

        return book;
    }
}

 

controller代码

@PostMapping("/bulkImportAdsource")
  public Response bulkImportAdsource(MultipartHttpServletRequest request) throws Exception {
		//前端传递参数名为  file  parameter
      MultipartFile file = request.getFile("file");
      String parameter = request.getParameter("parameter");
      AdSourceVo adSourceVo = JSON.parseObject(parameter, AdSourceVo.class);

      if (file == null) {
          return new Response<>(ResponseEnum.NON_File);
      }

      String fileName = file.getOriginalFilename();
      assert fileName != null;
		//解析表格数据,ExcelUtils工具类在上边
      List> list = ExcelUtils.getListByExcel(file.getInputStream(), fileName);
      String result = aggregateService.bulkImportAdsource(list,adSourceVo);
      if (org.apache.commons.lang3.StringUtils.isBlank(result)){
          return new Response<>(ResponseEnum.SUCCESS);
      }else {
          return new Response<>(ResponseEnum.PART_SUCCESS,result);
      }

  }

实现类代码

@Override
public String bulkImportAdsource(List> list, AdSourceVo adSourceVo) {
    //封装要导入的代码位list
    List sourceList = new ArrayList<>();

    //遍历解析后的表格数据
    for (int i = 1 ; i 
        List objects = list.get(i);
        AdSourceVo vo = new AdSourceVo();
        vo.setPlaceId(adSourceVo.getPlaceId());
        vo.setGroupId(adSourceVo.getGroupId());

        XSSFCell xssfCell = (XSSFCell) objects.get(1);
        xssfCell.setCellType(CellType.STRING);
        vo.setPlatPlaceId(xssfCell.getStringCellValue());

        vo .setTestId(adSourceVo.getTestId());
        vo.setPlatId(adSourceVo.getPlatId());

        xssfCell = (XSSFCell) objects.get(2);
        xssfCell.setCellType(CellType.STRING);
        vo.setName(xssfCell.getStringCellValue());

        xssfCell = (XSSFCell) objects.get(3);
        xssfCell.setCellType(CellType.STRING);
        if ("固价".equals(xssfCell.getStringCellValue())){
            vo.setSettleType(0);

            xssfCell = (XSSFCell) objects.get(4);
            xssfCell.setCellType(CellType.STRING);
            vo.setBidfloor(new BigDecimal(xssfCell.getStringCellValue()));
        }else {
            vo.setSettleType(1);
        }

        xssfCell = (XSSFCell) objects.get(5);
        xssfCell.setCellType(CellType.STRING);
        if ("开启".equals(xssfCell.getStringCellValue())){
            vo.setSts("A");
        }else {
            vo.setSts("S");
        }
        sourceList.add(vo);
    }
    adSourceVo.setSourceData(sourceList);
    //业务代码
    return batchAddSource(adSourceVo);
}



转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号