博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC文件上传
阅读量:7005 次
发布时间:2019-06-27

本文共 789 字,大约阅读时间需要 2 分钟。

十、文件上传

1.需要导入两个jar包

2.在SpringMVC配置文件中加入

3.方法代码

@RequestMapping(value="/upload",method=RequestMethod.POST)    public String upload(HttpServletRequest req) throws Exception{        MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)req;        MultipartFile file = mreq.getFile("file");        String fileName = file.getOriginalFilename();        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");                FileOutputStream fos = new FileOutputStream(req.getSession().getServletContext().getRealPath("/")+                "upload/"+sdf.format(new Date())+fileName.substring(fileName.lastIndexOf('.')));        fos.write(file.getBytes());        fos.flush();        fos.close();                return "hello";    }

4.前台form表单

转载地址:http://uautl.baihongyu.com/

你可能感兴趣的文章