springboot 文件和表单一起提交
真的不得不说一句,网上搜了一大圈。没搜见一个合适的。。。
- 1、首先是
文件上传
–网上随便找一个:
@Controller
public class FileController {
@GetMapping("/upload/page")
public String uploadPage(){
return "user-list";
}
@PostMapping("/upload/mutipart")
public Map<String,Object> upload(@RequestParam("photo") MultipartFile photo){
String path = "D:/桌面文件/图片/Springboot";//保存路径
String filename = new
SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
//获取上传文件的后缀suffix
String suffix = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf("."));
try {
//Spring提供了文件操作类FileCopyUtils
FileCopyUtils.copy(photo.getInputStream(), new
FileOutputStream(path + filename + suffix));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("上传失败");
return dealResultMap(false, "上传失败");
}
System.out.println("上传成功");
return dealResultMap(true, "上传成功");
}
private Map<String, Object> dealResultMap(boolean success, String msg)
{
Map<String, Object> result = new HashMap<String, Object>();
result.put("success", success);
result.put("msg", msg);
return result;
}
}
- 场景切入:我要在 添加图书时,加入
文件上传
:
(图片简陋,不要介意)
(1)表单如下:(我用的
jsp
)
去掉子表单,
将原来的: enctype="multipart/form-data"
统一放到上面,统一提交到:/Book/addBook
<form method="post"
action="/upload/mutipart" enctype="multipart/form-data">
<input type="file" name="photo" value="请选择上传的文件" />
<input type="submit" value="提交" />
</form>
表单与 文件上传 结合:
<form action="${pageContext.request.contextPath}/Book/addBook" method="post" enctype="multipart/form-data">
<table class="table table-bg">
<tbody>
<tr>
<th width="100" class="text-r"><span class="c-red">*</span> 书名:</th>
<td><input type="text" style="width:200px" class="input-text" value="" placeholder="" id="user-name" name="bookName" datatype="*2-16" nullmsg="书名不能为空" required></td>
</tr>
<tr>
<th class="text-r"><span class="c-red">*</span> 图片:</th>
<td>
<%-- 注释 <form method="post"--%>
<%-- 注释了哦 action="/Book/addBook" >--%>
<input type="file" name="photo" value="请选择上传的图片" />
<br>
</td>
</tr>
<tr>
<th class="text-r">价格:</th>
<td><input type="text" style="width:300px" class="input-text" value="" placeholder="" id="user-tel" name=price required></td>
</tr>
<tr>
<th></th>
<td><button class="btn btn-success radius" type="submit"><i class="icon-ok"></i> 确定</button></td>
<a href="${pageContext.request.contextPath}/Book/queryAll" class="btn btn-primary radius"> 返回</a></span>
</tr>
</tbody>
</table>
</form>
(2)
Controller
层代码如下:
- 传递参数:
MultipartFile photo
- 获取上传的文件名
- 设置要上传的文件路径
@RequestMapping("/addBook")
public String addBook(Book book,@RequestParam("photo") MultipartFile photo){
String path = "D:\\桌面文件\\图片\\Springboot\\";//保存路径
String filename = new
SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
//这个 看自己罢,想重置文件名,就写这个,不想就往下看:fileName
String fileName = photo.getOriginalFilename();
//获取上传文件的后缀suffix
String suffix = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf("."));
try {
FileCopyUtils.copy(photo.getInputStream(), new
FileOutputStream(path + fileName ));
} catch (IOException e) {
e.printStackTrace();
System.out.println("上传失败");
}
System.out.println("上传成功");
System.out.println(path + fileName );
System.out.println(book.getBookName());
if(book.getBookName()==null){
return "redirect:/Book/queryAll";
}
book.setImg("Springboot/"+fileName);
boolean b = bookService.addBook(book);
if(b){
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
return "redirect:/Book/queryAll";
}
- 尝试添加一下:
结果:
- 进行一点修改,可以在网页上显示:
感谢大佬 : springboot项目通过URL访问静态图片,本地文件夹下的图片.
呃,我好像忽略了什么:
OVER,写了很简单的文件上传,在此抛砖引玉