文件上传:
1:jar包 commons-fileupload
commons-io
2:配置文件:
<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
3:上传页面
<form action="upload.action" method="post" enctype="multipart/form-data">
上传文件:<input type="file" name="myfile" ><br>
<input type="submit" value="提交">
</form>
4:处理器wen层
第一个是上传文件,第二个是上传文件,同时把名字存起来,然后在jsp界面调用
@RequestMapping("/upload")
public String upload(@RequestParam("myfile") MultipartFile file,HttpServletRequest res,Model model) throws FileNotFoundException, IOException{
//写到服务器中
File path = new File("F:/"+file.getOriginalFilename());
Streams.copy(file.getInputStream(), new FileOutputStream(path),true);
return "succ";
}
@RequestMapping("/upload1")
public String upload1(@RequestParam("myfile1") MultipartFile file,HttpServletRequest res,Model model) throws FileNotFoundException, IOException{
String realpath = res.getServletContext().getRealPath("img");
//写到服务器中
File path = new File(realpath+"/"+file.getOriginalFilename());
model.addAttribute("imgsrc", file.getOriginalFilename());
Streams.copy(file.getInputStream(), new FileOutputStream(path),true);
return "succ";
}