springmvc 表单 @RequestMapping 上传文件


@RequestMapping(method = RequestMethod.POST)
public String save(
@RequestParam(value = "file", required = false) MultipartFile file,
@Valid Store store, BindingResult result) {
if (result.hasErrors()) {
return "admin/store/create";
}
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
String path = UploadFileUtil.saveFile(bytes);
store.setLogo(path);
} catch (IOException e) {
e.printStackTrace();
}
}
store.setCreateTime(new Date());
storeService.save(store);
return "redirect:/admin/store/" + store.getId();
}




网上看到的都是一个文件上传的例子,实际很多时候是与model一起提交上来的,今天试了一下,发现这2个参数的位置有讲究,换一下,就出错了。大致的错误是验证了错误,但是不能正常的返回提交页面,显示错误。具体原因没有调查


@RequestParam(value = "file", required = false) MultipartFile file
@Valid Store store



<html>

<head>
<title >layout title</title>
</head>

<body >
<form action="/admin/store" method="post" enctype="multipart/form-data">

<fieldset>
<legend >
添加 </legend>
<p>
<label >名称:</label>
<input type="text" id="name" name="name" value="" > </p>

<p>
<label >域名: </label>
<input type="text" id="domainName" name="domainName" value="" > </p>

<p>
<label >地址: </label>
<input type="text" id="address" name="address" value="" > </p>

<p>
<label >商标: </label>
<input type="file" name="file"/>
</p>

<p>
<label > </label>
<input type="submit" value="添加"/>
</p>

</fieldset>

</form>
</body>
</html>



[url=http://www.chinab2c.net/content/71.html]网上超市平台BOSS系统技术方案介绍[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值