zoukankan      html  css  js  c++  java
  • Spring MVC文件上传

    导入依赖:

    <groupId>commons-io</groupId>
              <artifactId>commons-io</artifactId>
              <version>2.4</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
            <dependency>
              <groupId>commons-fileupload</groupId>
              <artifactId>commons-fileupload</artifactId>
              <version>1.3.1</version>
            </dependency>

    jsp页面

    <form method="post" action="/file/fileUploads" enctype="multipart/form-data">
        <input type="file" name="file"/>
        作者<input type="text" name="author"/>
        <input type="submit" class="submit"/>
    </form>

    Controller层

    @RequestMapping("/fileUpload")
    public String fileupload(HttpSession session, MultipartFile file,String  anthor) throws IOException {
        System.out.println("作者:"+anthor);
        System.out.println(file);
        if (!file.isEmpty()){
            //获取文件名称
            String fileName=file.getOriginalFilename();
            //获取到需要上传的路径
            String realPath=session.getServletContext().getRealPath("/WEB-INF/upload");
            //创建文件对象
            File uploadfile=new File(realPath+"\"+fileName);
            //如何上传
            file.transferTo(uploadfile);
    
        }
        return "index";
    }

    编写spring-mvc.xml文件

    <!--文件上传解析器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSizePerFile" value="100000000"></property>
        <property name="maxUploadSize" value="5000000000"></property>
    </bean>
  • 相关阅读:
    MySQL的小Tips
    Linux中Eclipse下搭建Web开发环境
    SaaS的那些事儿
    典型的软件过程模型
    浅谈「敏捷」开发
    软件工程的本质
    堆和优先队列
    c 判断文件或文件夹是否存在,多种方法, 为什么从一开始就不直接来个统一的呢?
    大江河流尽
    jpg、png格式的图片转换成webp后颜色失真的问题
  • 原文地址:https://www.cnblogs.com/cw172/p/11838737.html
Copyright © 2011-2022 走看看