SpringMVC文件上传

单文件上传

第①步:引入jar包

<!--文件上传的jar包-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.4</version>
        </dependency>

  

第②步:创建form表单,method必须为POST,并加入属性enctype="multipart/form-data"

<%--单文件上传--%>
<form action="/first" method="post" enctype="multipart/form-data">
    请选择上传文件:<input type="file" name="upload"/><br/>
    <input type="submit"/>
</form>

  

第③步:创建处理器

@Controller
public class FirstController {


    /**
     * upload   文件对象
     */
    @RequestMapping("/first")
    public String doFirst(MultipartFile upload, HttpSession session){
        //判断是否为空文件
        if(upload.getSize()>0){
            //获取文件名
            String filename = upload.getOriginalFilename();
            //指定上传文件格式
            if (filename.endsWith("jpg")||filename.endsWith("JPG")){
                //获取全路径
                String realPath = session.getServletContext().getRealPath("/upload");
                //拼接全路径
                File file=new File(realPath,filename);
                //上传文件
                try {
                    upload.transferTo(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
      //返回页面 return "index"; } }

  

第④步:配置xml文件

 <!--:参数方法名解析器-->
   <bean id="methodNameResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/"/>
       <property name="suffix" value=".jsp"/>
   </bean>
    <!--扫描包下所有的被标注的类-->
    <context:component-scan base-package="day16Fileupload"/>

    <!--id必须为multipartResolver   它是DispatchServlet的常量值-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--表单上传文件总大小,字符为单位-->
        <property name="maxUploadSize" value="20971520"/>
        <!--设置编码格式-->
        <property name="defaultEncoding" value="utf-8"/>
    </bean>

    <!--配置注解驱动-->
    <mvc:annotation-driven/>

  

多文件上传

和单文件上传差不多

<form action="/second" method="post" enctype="multipart/form-data">
    请选择上传文件1:<input type="file" name="upload"/><br/>
    请选择上传文件2:<input type="file" name="upload"/><br/>
    请选择上传文件3:<input type="file" name="upload"/><br/>
    <input type="submit"/>
</form>

  

处理器方法

/*多文件上传,使用MultipartFile数组接收*/
    @RequestMapping("/second")
    public String doSecond(@RequestParam MultipartFile[] upload, HttpSession session){
        //循环上传
        for (MultipartFile item:upload){
            //获取文件名
            String filename = item.getOriginalFilename();
            //获取全路径
            String realPath = session.getServletContext().getRealPath("/upload");
            //拼接全路径
            File file=new File(realPath,filename);
            //上传文件
            try {
                item.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //返回视图
        return "index";
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木易学长~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值