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

所属分类: 软件编程 / java 阅读数: 52
收藏 0 赞 0 分享

这篇文章主要介绍了通过简单步骤实现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>

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

更多精彩内容其他人还在看

Springmvc restful配置遇到的小坑

本文是小编给大家带了的Springmvc restful配置遇到的小小坑,小编给大家带来了问题原因及解决办法,非常不错,具有参考借鉴价值,感兴趣的朋友一起看下吧
收藏 0 赞 0 分享

Java中的匿名内部类小结

java内部类分为: 成员内部类、静态嵌套类、方法内部类、匿名内部类。这篇文章主要介绍了Java中的匿名内部类的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java的云打印Lodop

这篇文章主要介绍了Java的云打印Lodop 的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java线程池框架核心代码解析

这篇文章主要针对Java线程池框架核心代码进行详细解析,分析Java线程池框架的实现ThreadPoolExecutor,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java 交换两个变量的数值实现方法

下面小编就为大家带来一篇Java 交换两个变量的数值实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

全面了解JAVA_BaseDAO数据处理类

下面小编就为大家带来一篇全面了解JAVA_BaseDAO数据处理类。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java、python、JavaScript以及jquery循环语句的区别

本篇文章主要介绍java、python、JavaScript以及jquery的循环语句的区别,这里整理了它们循环语句语法跟示例,以便大家阅读,更好的区分它们的不同
收藏 0 赞 0 分享

基于JDBC封装的BaseDao(实例代码)

下面小编就为大家带来一篇基于JDBC封装的BaseDao(实例代码)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

简单通用JDBC辅助类封装(实例)

下面小编就为大家带来一篇简单通用JDBC辅助类封装(实例)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

浅谈java线程中生产者与消费者的问题

下面小编就为大家带来一篇浅谈java线程中生产者与消费者的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多