{{format('0')}} {{format('549')}} {{format('3121')}}

SpringMVC 文件上传 [ 技术杂谈 ]

九叶的小窝 文章 正文

As Long As You Love Me.
分享

九叶

{{nature("2020-12-26 13:52:37")}}更新

在SpringMVC中静态资源文件是五法直接被访问的, 需要在SpringMVC的配置文件中配置

所谓的静态资源文件主要指:js、css,img......等等

配置如下:底下的配置意思是代表,WebRoot底下的某个文件夹底下的所有文件

location表示位置

/ 表示该目录底下的所有文件

<mvc:resources location="/img/" mapping="/img/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/css" mapping="/css/**"/>

添加文件上传的解析器

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="5242800"/>
    <property name="defaultEncoding" value="UTF-8"/>
</bean>

页面

单文件

<form method="post" action="imgFileUpload" enctype="multipart/form-data">
    <input type="file" name="image"> <br/>
    <input type="submit" value="上传文件">
</form>

多文件

<form method="post" action="imgsFileUpload" enctype="multipart/form-data">
    <input type="file" name="image"> <br/>
    <input type="file" name="image"> <br/>
    <input type="file" name="image"> <br/>
    <input type="submit" value="上传文件">
</form>

控制器

单文件上传

    @RequestMapping(value = "imgFileUpload")
    public String imgFileUpload(@RequestParam(value = "image",required = true) MultipartFile image, HttpServletRequest request) throws IOException {
        //1.判断是否有文件传来
        if(image!=null){
            //2.设置上传路径
            String path = request.getSession().getServletContext().getRealPath("/") + "/img/";
            File dir = new File(path);
            if(!dir.exists()){
                dir.mkdirs();//如果这个路径不存在,则创建该目录
            }
            //3.转存文件
            // getOriginalFilename() 获取文件名
            File file = new File(path, image.getOriginalFilename());
            //分目录存放, 和文件名的处理, 在SpringMVC中一样需要
            image.transferTo(file);
        }
        return "success";
    }

多文件上传

    @RequestMapping(value = "imgsFileUpload")
    public String imgsFileUpload(@RequestParam(value = "image",required = true) MultipartFile[] images, HttpServletRequest request) throws IOException {
        //1.判断文件是否为空
        if(images!=null){
            //2.设置上传路径
            String path = request.getSession().getServletContext().getRealPath("/")+"/img/";
            File dir = new File(path);
            if(!dir.exists()){
                dir.mkdirs();//如果这个路径不存在,则创建该目录
            }
            //3.转存文件
            for (MultipartFile img:images) {
                File file = new File(path,img.getOriginalFilename());
                img.transferTo(file);
            }
        }
        return "success";
    }
评论 0
0
{{userInfo.data?.nickname}}
{{userInfo.data?.email}}
TOP 2
Minecraft | [1.12.2] 核电工艺模拟器 1.2.25 —— 汉化版

{{nature('2021-12-07 15:29:00')}} {{format('2396')}}人已阅读

TOP 3
SSM搭建Spring单元测试环境

{{nature('2021-01-31 20:01:00')}} {{format('1034')}}人已阅读

TOP 4
dispatcher-servlet.xml文件配置模板

{{nature('2020-12-11 21:17:00')}} {{format('947')}}人已阅读

TOP 5
Windows平台Nacos启动报错无法创建Bean实例

{{nature('2021-04-22 15:16:00')}} {{format('926')}}人已阅读

目录

标签云

SpringMVC 文件上传

一言

# {{hitokoto.data.from || '来自'}} #
{{hitokoto.data.hitokoto || '内容'}}
作者:{{hitokoto.data.from_who || '作者'}}
自定义UI
配色方案

侧边栏