• springmvc文件上传


    一、导入相关依赖(还需要配置好springmvc的环境)

    <!--文件上传-->
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.4</version>
        </dependency>

    二、配置springmvc.xml

    <!--配置文件解析器对象-->id是固定不变的
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!--最大值限制为10M-->
            <property name="maxUploadSize" value="10485760"></property>
        </bean>

    三、编写接收controller

    利用srpingmvc封装的MultipartFile对象

    @PostMapping(value = "/fileUpload")
        public void FileUpload(MultipartFile file) throws IOException {
            String fileName = file.getOriginalFilename();
            System.out.println("文件名 "+fileName);
            String path = request.getServletContext().getRealPath("");
            path = path + "upload/"+fileName;
            File filePath = new File(path);
            if (!filePath.exists()){
                filePath.mkdir();
            }
            System.out.println("保存路径 "+filePath);
         //直接执行保存操作 file.transferTo(filePath); }
  • 相关阅读:
    点击鼠标获得坐标位置
    广告的字一个一个的显示出来
    纯css实现下拉菜单的效果
    用css3写出的倒三角形
    MySQL(三)
    Navicat之MySQL连接(二)
    MySQL 的安装与使用(一)
    Servlet(二)
    Servlet(一)
    Linux常用命令大全
  • 原文地址:https://www.cnblogs.com/ushowtime/p/12221739.html
Copyright © 2020-2023  润新知