springMVC_08文件上传

一.步骤总结

  1. 导入jar包
  2. 配置web.xml
  3. 在src目录下创建配置文件mvc.xml
  4. 创建前段页面fileupload.jsp
  5. 创建controller类HelloController
  6. 配置mvc.xml文件
  7. 在web-inf目录下建立文件夹jsp,建立文件success.jsp

 

二.详细步骤

  1.导入jar包 

commons-logging-1.1.1.jar

jackson-annotations-2.5.4.jar

jackson-core-2.5.4.jar

jackson-databind-2.5.4.jar

spring-aop-4.1.6.RELEASE.jar

spring-beans-4.1.6.RELEASE.jar

spring-context-4.1.6.RELEASE.jar

spring-core-4.1.6.RELEASE.jar

spring-expression-4.1.6.RELEASE.jar

spring-tx-4.1.6.RELEASE.jar

spring-web-4.1.6.RELEASE.jar

spring-webmvc-4.1.6.RELEASE.jar

在此基础上再导入两个jar包

commons-fileupload-1.2.2.jar

commons-io-2.0.1.jar

 

 

  2.配置web.xml

  
<filter>
      <filter-name>encoding</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <init-param>
          <param-name>encoding</param-name>
          <param-value>utf-8</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>encoding</filter-name>
      <url-pattern>*.do</url-pattern>
  </filter-mapping>
  
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:mvc.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>

  </servlet>
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
web.xml关键代码

 

 

  3.在src目录下创建配置文件mvc.xml

 

  4.创建前段页面fileupload.jsp

    <form action="upload.do" method="post" enctype="multipart/form-data">
        <input type="file" name="file" value="选择文件"/>
        <input type="submit" value="submit"/>
    </form>

 

  

  5.创建controller类HelloController,红色部分为必须要有的

package com.ahd.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Controller
public class HelloController4{
    @RequestMapping("/upload")
    public String upload(@RequestParam("file")CommonsMultipartFile file,HttpServletRequest req,ModelMap mm) throws IOException{
        //获取上传路径
        String path=req.getRealPath("upload");
        //获取文件名称
        String filename=file.getOriginalFilename();
        
        InputStream is = file.getInputStream();
        OutputStream os = new FileOutputStream(new File(path,filename));
        
        byte[]b=new byte[1024];
        int len=0;
        
        while((len=is.read(b))!=-1){
            os.write(b, 0, len);
            
        }
        mm.addAttribute("msg", "文件上传成功");
        return "success";
    }
}

 

  

  6.配置mvc.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd ">
    <!-- 配置handerAdapter  适配器 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
   <!-- 文件上传配置 -->
    <bean id="multipartResolver"  
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设置编码格式 -->  
        <property name="defaultEncoding" value="utf-8"></property> 
        <!-- 设置文件大小 -->  
        <property name="maxUploadSize" value="10485760000"></property>
        <!-- 设置缓冲区大小 -->  
        <property name="maxInMemorySize" value="40960"></property>  
    </bean> 
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <!-- 将视图名 渲染后视图的前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 渲染后视图的后缀 -->
        <property name="suffix" value=".jsp"/>
        <!-- 例:视图名为:hello   渲染后:/WEB-INF/jsp/hello.jsp 该页面-->
    </bean>

    
    <!-- spring容器扫描指定包下的所有类,如果类上有注解  那么根据注解产生相应bean对象已经映射信息 -->
    <context:component-scan base-package="com.ahd.controller"/>
    
</beans>

 

 

  7.在web-inf目录下建立文件夹jsp,建立文件success.jsp

    success.jsp
    ${msg}

 

posted @ 2018-12-24 11:05  爱华顿g  阅读(349)  评论(0编辑  收藏  举报