Monday 2008年06月23日
annotation驱动的spring mvc文件上传
jsp文件:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="post" action="uploadfile.do" enctype ="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="upload"/>
</form>
</body>
</html>
controller:
@Controller
public class FileUploadController {
@RequestMapping("/editor/preuploadfile.do")
public String uploadrequest(){
return "/editor/uploadfile";
}
@RequestMapping("/editor/uploadfile.do")
public String processSubmit(@RequestParam("file") MultipartFile mfile ) throws IOException{
File file = new File(mfile.getOriginalFilename());
mfile.transferTo(new File("D:\\Temp\\"+file.getName()));
return "editor/uploadfinish";
}
}
xml配置项:
<!-- MultipartResolver for parsing file uploads, implementation for Commons FileUpload -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
简单得难以置信!
发表于 chsan
( 2008年06月23日, 02:42:23 PM CST )
Permalink