zoukankan      html  css  js  c++  java
  • springmvc复习笔记----文件上传multipartResolver

    结构

                                                


    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>springmvc01</display-name>
      <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
      </servlet>
      <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
      
      <filter>
        <filter-name>characterEncodingfilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
          <param-name>encoding</param-name>
          <param-value>utf8</param-value>
        </init-param>
      </filter>
      <filter-mapping>
      <filter-name>characterEncodingfilter</filter-name>
      <url-pattern>*</url-pattern>
      </filter-mapping>
    </web-app>
    View Code

    spring-mvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
            xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!--   自动扫描加载注解的包 -->
    <context:component-scan base-package="com.ij34.bean"/>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/view/"></property>
    <property name="suffix" value=".jsp" ></property>
    </bean>
    <bean id="multipartResolver"
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            
            <property name="defaultEncoding" value="UTF-8"/>  
            <property name="maxUploadSize" value="100000000000000"/>
    
        </bean>
    </beans>

    index.jsp 

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <form action="upload" method="post" enctype="multipart/form-data">
    <table>
    <tr>
    <th colspan="2">上传</th>
    </tr>
    <tr>
    <td>
    <input type="file" name="file">
    </td>
    </tr>
    <tr>
    <td>
    <input type="submit" value="提交">
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>

    success.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    upload success
    </body>
    </html>
    View Code

    Testhello.java

    package com.ij34.bean;
    
    import java.io.File;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.multipart.MultipartFile;
    
    @Controller
    public class Testhello {
    
      @RequestMapping("/upload")   //@RequestMapping  请求映射
      public String upload(@RequestParam("file") MultipartFile[] files,HttpServletRequest request)throws Exception {
        String path=request.getServletContext().getRealPath("/");
        System.out.println(path);
        for(MultipartFile file:files){
            file.transferTo(new File(path+"upload/"+file.getOriginalFilename()));
        }
        return "forward:success.jsp";
      
      }
    }

    结果

  • 相关阅读:
    UVA12125 March of the Penguins (最大流+拆点)
    UVA 1317 Concert Hall Scheduling(最小费用最大流)
    UVA10249 The Grand Dinner(最大流)
    UVA1349 Optimal Bus Route Design(KM最佳完美匹配)
    UVA1212 Duopoly(最大流最小割)
    UVA1395 Slim Span(kruskal)
    UVA1045 The Great Wall Game(二分图最佳匹配)
    UVA12168 Cat vs. Dog( 二分图最大独立集)
    hdu3488Tour(KM最佳完美匹配)
    UVA1345 Jamie's Contact Groups(最大流+二分)
  • 原文地址:https://www.cnblogs.com/tk55/p/6655862.html
Copyright © 2011-2022 走看看