
form java springmvc怎么上传文件
2017-01-09
@Controller
@RequestMapping("/saveloads")
public class testload {
@RequestMapping("/index")
public ModelAndView index(){
ModelAndView model=new ModelAndView("/user/index");
return model;
}
@RequestMapping("/saveload")
public ModelAndView upload(@RequestParam(value="fileload")MultipartFile file){
ModelAndView model=new ModelAndView("/user/result");
//拿到文件的名字
String fileName=file.getOriginalFilename();
System.out.println(fileName);
//转移目标
File tagetFile=new File("F:/image/",fileName);
try{
//将MUltipartfile转移到tagetFile
file.transferTo(tagetFile);
}catch(IllegalStateException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
model.addObject("fileUrl","/image/"+fileName);
return model;
}
}
写一个有上传功能的jsp页面,action自己改吧,
然后在Tomcat服务器中设置文件存储的位置,要和代码中写的位置一样,我写的是F:/image/