zoukankan      html  css  js  c++  java
  • spring+jpg环境下,spring实现文件上传

    jsp:

    <form method="post" action="excel.do?method=inputExcel" enctype="multipart/form-data">
           <table>
            <tr>
             <td>
              <input type="file" name="file"/>
             </td>
            </tr>
            <tr>
             <td>
              <input type="submit" value="上传"/>
             </td>
            </tr>
           </table>
          </form>

    前提:将以下两个jar包复制到WEB-INF 文件夹lib下:

    commons-io-1.3.2.jar

    commons-fileupload.jar

    public ModelAndView inputExcel(HttpServletRequest request, HttpServletResponse response){

     MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
     CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");
    // String name = multipartRequest.getParameter("name");
     String realFileName = file.getOriginalFilename();
     //System.out.println("获得文件名:" + realFileName); // 获取路径
     String ctxPath = request.getSession().getServletContext().getRealPath("/")  + "upload/";  // 创建文件
     File dirPath = new File(ctxPath);

    if (!dirPath.exists()) {
      dirPath.mkdir();
      }
     
     File uploadFile = new File(ctxPath + realFileName);
     FileCopyUtils.copy(file.getBytes(), uploadFile);

    }

    application.xml配置:

    <!--上传控制-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
    p:defaultEncoding="utf-8">
    <property name="maxUploadSize">
    <value>104857600</value>
    </property>
    <property name="maxInMemorySize">
    <value>4096</value>
    </property> 
    </bean> 

    即可!!

  • 相关阅读:
    Codevs 2602 最短路径问题
    NOIp2015酱油酱油记
    51Nod-1091 线段的重叠
    poj-3264-Balanced Lineup
    51Nod-1212 无向图最小生成树
    51Nod-1279 扔盘子
    51Nod--1010 只包含235的数
    51Nod--1015 水仙花数
    51Nod-1136 欧拉函数
    使用caffe训练自己的CNN
  • 原文地址:https://www.cnblogs.com/qgc88/p/3199588.html
Copyright © 2011-2022 走看看