SpringMVC(三)upload

导入 jar 包:commons-io-2.0.jar

这里写图片描述
用CommonsMultipartFile时必须是commons-io-2.0.jar以上版本

1.文件上传页面 jsp

<form action="upload.do" method="post" enctype="multipart/form-data">
        <table>
            <tr>
                <td>上传人:</td>
                <td><input type="text" name="user" value="holly"></td>
            </tr>
            <tr>
                <td>选择图片:</td>
                <td><input type="file" name="uploadfile"></td>
            </tr>
            <tr>
                <td colspan="2" align="center"></td>
                <td><input type="submit" value="提交"></td>
            </tr>
        </table>
      </form>

2.配置 springMVC-serlvet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
    <!-- 1.mvc注解驱动 -->
    <mvc:annotation-driven/>
    <!-- 2.全局扫描包 -->
    <context:component-scan base-package="com"/>
    <!-- 上传下载配置 :公共的二进制处理器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 上传时默认的编码格式 -->
        <property name="defaultEncoding"  value="UTF-8"/>
        <!--指定上传的总的文件大小的总和。默认是200kb。-1是不设限  -->
        <property name="maxUploadSize" value="-1"/>
    </bean>

    <!--
       简单的映射异常 :文件超过指定大小的异常配置
      注意:id的值是固定的
    -->
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">
                    error.jsp
                </prop>
            </props>
        </property>
    </bean>
</beans>

3. controller

@Controller
public class UploadController {

    /*
     * 用CommonsMultipartFile时必须是commons-io-2.0.jar以上版本
     */
    @RequestMapping("/upload.do")
    public String queryFileDate(HttpServletRequest request,@RequestParam("uploadfile") CommonsMultipartFile file) throws IOException{

        //判断文件对象是否存在
        if (file!=null) {

            // 1.获取文件名
            String filename=file.getOriginalFilename();
            System.out.println("filename:"+filename);

            // 2. 获取上传的文件路径
            String path=request.getSession().getServletContext().getRealPath("upload/"+filename);
            System.out.println("path:"+path);

            // 3.创建文件流指定写入路径
            File uploadpath=new File(path);

            // 4.上传文件
            FileUtils.copyInputStreamToFile(file.getInputStream(), uploadpath);
            System.out.println("上传成功!");

            return "/success.jsp";
        }else {
            System.out.println("文件对象没有找到!");
        }
        return "/error.jsp";

    }

4.success.jsp和 error.jsp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值