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

    ----------------------siwuxie095

       

       

       

       

       

       

       

    SpringMVC 文件上传

       

       

    1、导入文件上传的 jar 包

       

    1Commons FileUpload

       

    https://commons.apache.org/proper/commons-fileupload/download_fileupload.cgi

       

       

    2Commons IO

       

    https://commons.apache.org/proper/commons-io/download_io.cgi

       

       

       

    2、配置文件上传解析器

       

    SpringMVC 核心配置文件 dispatcher-servlet.xml 中添加如下内容:

       

    <!-- 配置 MultipartResolver -->

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- 配置默认编码为 UTF-8 -->

    <property name="defaultEncoding" value="UTF-8"></property>

    <!-- 配置文件上传的最大值为 5 MB,这里单位为字节,所以是 5*1024*1024 -->

    <property name="maxUploadSize" value="5242880"></property>

    </bean>

       

       

       

       

    3、编写一个文件上传的 JSP 页面

       

    test.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>test</title>

    </head>

    <body>

       

    <%-- <%=request.getContextPath()%> 等同于 ${pageContext.request.contextPath} --%>

    <form action="<%=request.getContextPath()%>/test.do" method="post" enctype="multipart/form-data">

    <input type="file" name="file"/>

    <input type="submit" value="提交"/>

    </form>

     

    </body>

    </html>

       

       

    注意两点:

       

    1)form 表单的 method 属性必须是 post

       

    2)form 表单的 enctype 属性必须是 multipart/form-data

       

       

       

    3、编写一个处理业务逻辑的 Controller 类

       

    TestController.java:

       

    package com.siwuxie095.controller;

       

    import java.io.File;

    import java.io.IOException;

       

    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 TestController {

       

    /**

    * 这里返回值是 String 类型,可以看做是特殊的 ModelAndView

    * 只有 ViewName,而没有 Model

    *

    * 返回的 ViewName redirect: 开头,表示做页面重定向

    */

    @RequestMapping("/test")

    public String test(@RequestParam("file") MultipartFile multipartFile) {

     

    try {

     

    if (multipartFile!=null) {

     

    String fileName = multipartFile.getOriginalFilename();

    /*

    * 也可以写成 F:\TEMP\ 注意:需要先在 F 盘创建 TEMP 文件夹

    */

    File file = new File("F:/TEMP/" + fileName);

    /*

    * 调用 transferTo() 方法上传文件

    */

    multipartFile.transferTo(file);

     

    return "redirect:/succ.jsp";

    }

     

    } catch (IllegalStateException e) {

    e.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();

    }

     

    return "redirect:/err.jsp";

    }

     

    }

       

       

    注意:返回的 String 是视图名称,它可以看做是一种特殊的 ModelAndView,

    只有视图名称,没有模型数据

       

    另外:返回的视图名称如果以 redirect: 开头,就做页面重定向

       

       

     

    参考链接:

       

    参考链接1参考链接2参考链接3参考链接4参考链接5

       

       

     

     

     

    补:

       

    SpringMVC 中 Controller 类的方法的返回值类型:

       

    1ModelAndView

       

    2Model

       

    3ModelMap

       

    4Map

       

    5View

       

    6String

       

    7void

       

       

    参考链接:

       

    参考链接1参考链接2参考链接3

       

       

       

       

       

       

       

       

    【made by siwuxie095】

  • 相关阅读:
    谈敏捷的好文章
    梁信军说的话
    如何做需求管理
    支持向量机通俗解释
    如何写数据报告
    数据分析注意点
    傅盛谈管理的本质
    I Hate It HDU
    敌兵布阵 HDU
    P3372 【模板】线段树 1 (区间查询)
  • 原文地址:https://www.cnblogs.com/siwuxie095/p/8488382.html
Copyright © 2011-2022 走看看