注册并上传页面
<form action="upload" method="post" enctype="multipart/form-data">
账户:<input type="text" name="username"/><br/>
密码:<input type="password" name="pwd"/><br/>
文件:<input type="file" name="mf"/><br/>
<input type="submit" value="注册"/>
</form>
控制器代码如下:
//3.上传案例
@RequestMapping("/upload")
public String register(User u,Model model,
@RequestParam("mf") MultipartFile mf){
System.out.println("name="+u.getUsername()+ ",pwd="+u.getPwd());
model.addAttribute("msg", "注册成功");
userList.add(u);
try { FileUtils.copyInputStreamToFile(
mf.getInputStream(),
new File("E:/"+mf.getOriginalFilename()));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(mf.getOriginalFilename()+
","+mf.getSize());
return "login";
}
spring-mvc配置文件,增加如下代码:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="10000000"/>