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

一、 前端表单文件

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

    <form  id="saveAgent" action="saveAgent" method="POST" enctype="multipart/form-data">
        代理人名称:<input type="text" name="agentName" />
        代理人手机号:<input type="text" name="agentPhone" />
        营业执照:<input type="file" name="blFile" />
        <input type="submit" value="提交" id="submitBnt">
    </form>

二、后端接收实体类

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<Object> 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<Object> 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

 

  • 15
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值