温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

通过简单步骤实现SpringMVC文件上传

发布时间:2020-08-20 19:31:15 来源:脚本之家 阅读:131 作者:EXTRA· 栏目:编程语言

这篇文章主要介绍了通过简单步骤实现SpringMVC文件上传,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、创建文件上传FileController类

package com.byzore.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/file")
public class FileController {
  @RequestMapping("/fileUpload")
  /**
   * MultipartFile 选择文件
   */
  public String fileupload(HttpSession session, MultipartFile file,String author)throws IOException{
    System.out.println("作者:"+author);
    System.out.println(file);
    /**
     * 如何处理文件
     */
    if (!file.isEmpty()){
      //获取文件名称
      String fileName=file.getOriginalFilename();
      //获取到需要上传的路径
      String realPath = session.getServletContext().getRealPath("/WEB-INF/upload");
      //创建文件对象
      File uploadfile=new File(realPath+"\\"+fileName);
      //如何上传文件
      file.transferTo(uploadfile);
    }
    return "index";
  }



  @RequestMapping("/fileUploads")
  /**
   * 多文件上传
   */
  public String fileuploads(HttpSession session, MultipartFile[] uploadFiles,String author)throws IOException{
    System.out.println("作者:"+author);
    System.out.println(uploadFiles);
    for (MultipartFile file: uploadFiles) {
      /**
       * 如何处理文件
       */
      if (!file.isEmpty()){
        //获取文件名称
        String fileName=file.getOriginalFilename();
        //获取到需要上传的路径
        String realPath = session.getServletContext().getRealPath("/WEB-INF/upload");
        //创建文件对象
        File uploadfile=new File(realPath+"\\"+fileName);
        //如何上传文件
        file.transferTo(uploadfile);
      }
    }

    return "index";
  }
}

二、编辑applicationContext.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:aop="http://www.springframework.org/schema/aop"
      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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--将Controller注入到容器当中  id就是浏览器请求地址-->
    <!--<bean id="/firstController" class="com.springmvc.controller.FirstController"></bean>-->

    <!--配置包扫描器-->
    <context:component-scan base-package="com.byzore"/>
    <!--Spring支持SpringMVC-->
    <mvc:annotation-driven/>

  <!--配置视图解析器-->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"/>
    <property name="suffix" value=".jsp"/>
  </bean>

  <!--利用DefaultServlet放行资源-->
  <mvc:default-servlet-handler/>

  <!--从Spring3.0.4版本提供资源放行的方式-->
  <!--<mvc:resources mapping="/**" location="/img"/>-->
<!--文件上传解析器-->
  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!--编码-->
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="maxUploadSize" value="5000000000"/>
  </bean>
</beans>

三、创建fileUpload.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>文件上传</title>
</head>
<body>
  <form action="/file/fileUpload" method="post" enctype="multipart/form-data">
    <input type="file" name="fileUpload"/>
作者:<input type="text" name="author"/>
    <input type="submit" value="提交"/>
  </form>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI