一、 前端表单文件
重点:设置enctype="multipart/form-data"
<form 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="提交" >
</form>
二、后端接收实体类
package ;
import ;
import ;
import ;
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) {
= id;
}
public String getAgentName() {
return agentName;
}
public void setAgentName(String agentName) {
= agentName == null ? null : ();
}
public String getAgentPhone() {
return agentPhone;
}
public void setAgentPhone(String agentPhone) {
= agentPhone == null ? null : ();
}
public MultipartFile getBlFile() {
return blFile;
}
public void setBlFile(MultipartFile blFile) {
= blFile;
}
}
三、后端控制类
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import .*;
import ;
@Api(value = "一级代理控制类")
@RestController
public class AgentInfoController {
@Autowired
private AgentService agentService;
@ApiOperation(value = "添加一级代理接口",notes = "添加一级代理接口",response = )
@PostMapping("saveAgent")
public ResultBundle<Object> saveAgent(AgentInfo agentInfo){
return (agentInfo);
}
}
四、后端业务类
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@Service
public class AgentServiceImpl implements AgentService {
@Autowired
private AgentInfoMapper agentInfoMapper;
@Override
@Transactional
public ResultBundle<Object> saveAgent(AgentInfo agentInfo) throws Exception{
if(agentInfo==null){
return (ResCode.FALSE_DATA,"一级代理信息不能为空");
}
MultipartFile blFile = ();
if(!()){
String oldFileName = ();
String path = ().getResource("").getPath()+"static/upload/bl";
String randomStr = ().toString();
String newFileName = randomStr + (("."));
File file = new File(path,newFileName);
if(!().exists()){
().mkdirs();
}
(file);
(path+"/"+newFileName);
}
int rowNum = (agentInfo);
if(rowNum==0){
return (,"添加一级代理信息失败");
}
return (null);
}
}
五、配置类
spring:
servlet:
multipart:
max-file-size: 2MB