码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMvc文件上传

时间:2018-12-06 22:48:20      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:控制   use   user   view   cep   order   类型   tab   等于   

1. 配置Spring 文件类型解析器

     <!--配置文件类型解析器-->
    <!--这里的id 名称不能修改,因为在中央控制器中有指定的名称-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--大小 约等于 10M   以kb为单位-->
        <property name="maxUploadSize" value="10240000"/>
    </bean>

2. 配置单文件上传

2.1.前端上传代码

<%--action前面的"/"  加上表示使用绝对路径,反之为相对路径--%>
<form action="/getFile.do" method="post" enctype="multipart/form-data">
    美女图片:
    <input type="file" name="imgCard"><br>
    <input type="su">
</form>

2.2 开发Action

@RequestMapping("getFile.do")
    public ModelAndView getFile(HttpServletRequest request, @RequestParam("imgCard") MultipartFile multipartFile) throws IOException {
        String name = request.getParameter("name");
        System.out.println("name = " + name);
        //得到文件的名称;
        String originalFilename = multipartFile.getOriginalFilename();
        //希望存储的目标地址
        File file = new File("C:\\Users\\lyuweigh\\Desktop\\"+originalFilename);
        multipartFile.transferTo(file);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index.jsp");
        mv.addObject("demo", "ojbk");
        return mv;
    }
  • MultipartFile ,用来存储对应上传的文件
  • @RequestParam("xx") ,接受指定的name名称值

3. 配置多文件上传

3.1.前端上传代码

<form action="/getFiles.do" method="post" enctype="multipart/form-data">
    <table border="1">
        <tr>
            <td>文件名称:</td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="file" name="imgCards"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="file" name="imgCards"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="file" name="imgCards"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="file" name="imgCards"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="提交"></td>
        </tr>
    </table>
</form>

3.2 开发Action

   @RequestMapping("getFiles.do")
    public ModelAndView getFiles(HttpServletRequest request, @RequestParam("imgCards") CommonsMultipartFile[] files) throws IOException {
        String name = request.getParameter("name");
        System.out.println("name = " + name);
        for (CommonsMultipartFile file : files) {
            String originalFilename = file.getOriginalFilename();
            file.transferTo(new File("C:\\Users\\lyuweigh\\Desktop\\"+originalFilename));
        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index.jsp");
        mv.addObject("demo", "ojbks");
        return mv;
    }

SpringMvc文件上传

标签:控制   use   user   view   cep   order   类型   tab   等于   

原文地址:https://www.cnblogs.com/lyuweigh/p/10080095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!