springmvc文件上传

来源:互联网 发布:乡镇网络舆情应急预案 编辑:程序博客网 时间:2024/06/05 09:56

===============fileup.jsp页面主要代码================

<body>    单个文件    <form action="${pageContext.request.contextPath}/fileup" method="post"        enctype="multipart/form-data">        <input type="file" name="photo"><br /> <input type="submit"            value="上传">    </form>    多个文件    <form action="${pageContext.request.contextPath}/morefileup"        method="post" enctype="multipart/form-data">        <input type="file" name="photos"><br /> <input type="file"            name="photos"><br /> <input type="file" name="photos"><br />        <input type="submit" value="上传">    </form></body>

===============单个,多个文件上传的控制器方法!上传成功跳转到ok.jsp=========

    /**     * 单个文件上传     * @param photo 照片文件     * @param session session对象     * @return ModelAndView     * @throws IllegalStateException 异常     * @throws IOException 异常     */    @RequestMapping(value="/fileup",method=RequestMethod.POST)    public ModelAndView doFileUp(@RequestParam MultipartFile photo,HttpSession session) throws IllegalStateException, IOException{        System.out.println("file");        if (!photo.isEmpty()) {  //判断文件不为空            System.out.println("Photo");            //服务端的image目录手动创建            //path=D:\apache-tomcat-7.0.73\webapps\SpringMVC4.3.7\image            //D:\\Eclipseworkspace\\SpringMVC4.3.7\\WebContent\\image            String path = session.getServletContext().getRealPath("/image");            //获取原始文件名            String fileName = photo.getOriginalFilename();            //限制文件上传格式            if (fileName.endsWith(".jpg") || fileName.endsWith(".png")) {                File file = new File(path, fileName);                //判断路径是否存在                if (!file.getParentFile().exists()) {                    file.getParentFile().mkdirs();                }                //完成上传                photo.transferTo(file);            } else{                return new ModelAndView("fileup.jsp");            }        }        return new ModelAndView("ok.jsp");    }    /**     * 多个文件上传     * @param photos 照片文件数组     * @param session session对象     * @return ModelAndView     * @throws IllegalStateException 异常     * @throws IOException 异常     */    @RequestMapping(value="/morefileup",method=RequestMethod.POST)    public ModelAndView doMoreFileUp(@RequestParam MultipartFile[] photos, HttpSession session) throws IllegalStateException, IOException{            //服务端的image目录手动创建            //path=D:\apache-tomcat-7.0.73\webapps\SpringMVC4.3.7\image            String path = session.getServletContext().getRealPath("/image");            for (MultipartFile photo : photos) {                if (!photo.isEmpty()) {  //判断文件不为空                    System.out.println("Photo");                    //获取原始文件名                    String fileName = photo.getOriginalFilename();                    //限制文件上传格式                    if (fileName.endsWith(".jpg") || fileName.endsWith(".png")) {                        File file = new File(path, fileName);                        //判断路径是否存在                        if (!file.getParentFile().exists()) {                            file.getParentFile().mkdirs();                        }                        //完成上传                        photo.transferTo(file);                    } else{                        return new ModelAndView("fileup.jsp");                    }                }            }            return new ModelAndView("ok.jsp");    }

=================springmvc.xml中注册multipart解析器==================

<!-- 注册multipart解析器 id名固定,是由DispatcherServlet直接调用 -->    <bean id="multipartResolver"        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">        <!-- 设置编码格式 要与jsp界面一致 -->        <property name="defaultEncoding" value="utf-8"></property>        <!-- 限制大小10M[10485760] -->        <property name="maxUploadSize" value="10485760"></property>    </bean>

这样就可以了!注意要导入一些文件上传的包!我用的maven,如果你用的导包方式的话就自己加包,maven导包我也贴出来

        <!-- 上传文件包 -->        <dependency>            <groupId>commons-fileupload</groupId>            <artifactId>commons-fileupload</artifactId>            <version>1.3.2</version>        </dependency>
原创粉丝点击