• spring mvc file upload


    文件上传

      1.需要导入两个jar包

      2.在SpringMVC配置文件中加入

    1
    2
    3
    4
    <!-- upload settings -->
    <bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="102400000"></property>
    </bean>

      3.方法代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    @RequestMapping(value="/upload",method=RequestMethod.POST)
    public String upload(HttpServletRequest req) throws Exception{
        MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)req;
        MultipartFile file = mreq.getFile("file");
        String fileName = file.getOriginalFilename();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");       
        FileOutputStream fos = new FileOutputStream(req.getSession().getServletContext().getRealPath("/")+
                "upload/"+sdf.format(new Date())+fileName.substring(fileName.lastIndexOf('.')));
        fos.write(file.getBytes());
        fos.flush();
        fos.close();
         
        return "hello";
    }

      4.前台form表单

    1
    2
    3
    4
    <form action="mvc/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="file"><br>
        <input type="submit" value="submit">
    </form>
  • 相关阅读:
    dubbo
    maven
    vue
    SSM框架整合
    MyBatis数据表注解开发
    MyBatis多表操作xml方式
    MyBatis映射配置文件
    Mybatis核心配置文件,传统开发和代理开发(主流)
    SpringMVC高级
    SpringMVC基础
  • 原文地址:https://www.cnblogs.com/sddychj/p/6132741.html
Copyright © 2020-2023  润新知