spring mvc 怎么实现上传文件

2024-11-17 21:48:05
推荐回答(1个)
回答1:

在springmvc配置文件中这样配置:









页面:






后台:
@Controller
public class UploadAction implements ServletContextAware {
private ServletContext context;
@RequestMapping("/upload")
public String upload(HttpServletRequest request) throws Exception{
//转换请求为文件上传请求
MultipartHttpServletRequest mrequest=(MultipartHttpServletRequest)request;
MultipartFile mfile=mrequest.getFile("file");
if(!mfile.isEmpty()){//判断文件是否为空
path=context.getRealPath("/upload")+File.separator+mfile.getOriginalFilename();
File file=new File(path);
mfile.transferTo(file);//保存文件
}
return "跳转页面";
}}