前端新建表单上传到http://127.0.0.1:8081/crowdfunding/project/addProject,springboot接收表单
时间: 2023-07-10 15:36:42 浏览: 115
好的,前端新建表单上传到http://127.0.0.1:8081/crowdfunding/project/addProject,Spring Boot接收表单数据的代码如下:
前端表单代码:
```html
<form method="post" enctype="multipart/form-data" action="http://127.0.0.1:8081/crowdfunding/project/addProject">
<input type="text" name="projectName" value="项目名称" />
<input type="file" name="projectImage" />
<button type="submit">提交</button>
</form>
```
Spring Boot接收表单数据的代码:
```java
@PostMapping("/crowdfunding/project/addProject")
public String addProject(@RequestParam("projectName") String projectName,
@RequestParam("projectImage") MultipartFile projectImage) throws IOException {
// 处理业务逻辑
String originalFilename = projectImage.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
String newFileName = UUID.randomUUID().toString() + suffix;
File destFile = new File("/data/project/images/" + newFileName);
projectImage.transferTo(destFile);
// 返回处理结果
return "success";
}
```
其中,@RequestParam注解用于接收表单数据中的projectName参数,@RequestParam注解用于接收表单数据中的projectImage参数,MultipartFile是Spring Boot提供的用于处理文件上传的类,可以通过getOriginalFilename()方法获取文件名,通过transferTo()方法将文件保存到指定路径。
阅读全文