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

    十、文件上传

    1.需要导入两个jar包

    2.在SpringMVC配置文件中加入

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

    3.方法代码

    复制代码
        @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表单

          <form action="mvc/upload" method="post" enctype="multipart/form-data">
              <input type="file" name="file"><br>
              <input type="submit" value="submit">
          </form>
  • 相关阅读:
    洛谷 P2370 P2370 yyy2015c01的U盘
    洛谷 P1214 等差数列
    洛谷 P1483 序列变换
    洛谷 P2032 扫描
    洛谷 P2846 光开关
    洛谷 P2872 道路建设
    Codeforces Round #510 #C Array Product
    Codeforces Round #510 #B
    Codeforces Round #510 #A
    关于各种数字倍数的判断
  • 原文地址:https://www.cnblogs.com/sharpest/p/6213453.html
Copyright © 2011-2022 走看看