SpringBoot实现表单数据与文件同时上传

一、 前端表单文件

重点:设置enctype="multipart/form-data"

    
代理人名称: 代理人手机号: 营业执照:

二、后端接收实体类

package org.cm.channelmanage.entity;

import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.Date;

public class AgentInfo implements Serializable {
    private Integer id;

    private String agentName;

    private String agentPhone;

    private MultipartFile blFile;

    private static final long serialVersionUID = 1L;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAgentName() {
        return agentName;
    }

    public void setAgentName(String agentName) {
        this.agentName = agentName == null ? null : agentName.trim();
    }

    public String getAgentPhone() {
        return agentPhone;
    }

    public void setAgentPhone(String agentPhone) {
        this.agentPhone = agentPhone == null ? null : agentPhone.trim();
    }


    public MultipartFile getBlFile() {
        return blFile;
    }

    public void setBlFile(MultipartFile blFile) {
        this.blFile = blFile;
    }

}

三、后端控制类

package org.cm.channelmanage.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.cm.channelmanage.entity.AgentInfo;
import org.cm.channelmanage.result.ResultBundle;
import org.cm.channelmanage.service.AgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api(value = "一级代理控制类")
@RestController
public class AgentInfoController {

    @Autowired
    private AgentService agentService;

    @ApiOperation(value = "添加一级代理接口",notes = "添加一级代理接口",response = ResultBundle.class)
    @PostMapping("saveAgent")
    public ResultBundle saveAgent(AgentInfo agentInfo){
        return agentService.saveAgent(agentInfo);
    }
}
 
  

四、后端业务类

package org.cm.channelmanage.service.impl;

import org.cm.channelmanage.common.constant.ResCode;
import org.cm.channelmanage.dao.xl.AgentInfoMapper;
import org.cm.channelmanage.entity.AgentInfo;
import org.cm.channelmanage.result.ResultBundle;
import org.cm.channelmanage.service.AgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
public class AgentServiceImpl implements AgentService {

    @Autowired
    private AgentInfoMapper agentInfoMapper;

    @Override
    @Transactional
    public ResultBundle saveAgent(AgentInfo agentInfo) throws Exception{
        if(agentInfo==null){
            return ResultBundle.failure(ResCode.FALSE_DATA,"一级代理信息不能为空");
        }

        MultipartFile blFile = agentInfo.getBlFile();
        if(!blFile.isEmpty()){
            String oldFileName = blFile.getOriginalFilename();
            String path = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static/upload/bl";
            String randomStr = UUID.randomUUID().toString();
            String newFileName = randomStr + oldFileName.substring(oldFileName.lastIndexOf("."));
            File file = new File(path,newFileName);
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdirs();
            }
            blFile.transferTo(file);
            agentInfo.setBusinessLicense(path+"/"+newFileName);
        }

        int rowNum  = agentInfoMapper.insertSelective(agentInfo);
        if(rowNum==0){
            return ResultBundle.failure(ResCode.FALSE,"添加一级代理信息失败");
        }
        return ResultBundle.success(null);
    }
}
 
  

五、配置类application.yml

spring:
  servlet:
    multipart:
      max-file-size: 2MB

 

你可能感兴趣的:(文件上传)