Springmvc实现文件上传

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

本文实例为大家分享了Springmvc实现文件上传的具体代码,供大家参考,具体内容如下

1.环境搭建:

在maven的pom.xml文件中导入两个依赖

1).commons-fileupload
2).commons-io

在resources目录下的springmvc.xml文件中配置multipartResolver

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
  <!--包扫描-->
  <context:component-scan base-package="cn.itcast"></context:component-scan>
  <!--配置multipartResolver,注意:id名称固定为multipartResolver-->
  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

  </bean>

  <mvc:annotation-driven ></mvc:annotation-driven>
  
  <!--让静态资源不经过过滤器-->
  <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>

  <!--视图解析器给controller中返回的逻辑视图名加上前缀和后缀-->
  <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"></property>
    <property name="suffix" value=".jsp"></property>
  </bean>
</beans>

2.编写前台测试jsp

<form action="/test/file" method="post" enctype="multipart/form-data">
    上传的文件:<input type="file" name="upload"><br/>
    密钥:<input type="text" name="password"><br/>
    <input type="submit" value="提交">
 </form>

注意页面三要素:

      1).表单提交方式必须为post

      2).表单中必须有file域,即type="file"

      3).表单中enctype="multipart/form-data"

3.编写后台测试代码

@Controller
@RequestMapping("/test")
public class FileUploadController {
  @RequestMapping("/file")
  public String testFileUpload(HttpServletRequest request, MultipartFile upload) throws IOException { 
     //upload是表单中文件name属性值,必须保持一致
    System.out.println("testFileUpload...");
    String realPath = request.getSession().getServletContext().getRealPath("/uploads");
    File file = new File(realPath);
    if(!file.exists()){
      file.mkdirs();//创建文件夹
    }
    String filename = upload.getOriginalFilename(); //获取文件名
    String name = upload.getName();//获取表单中的name属性值 即upload

    String uuid = UUID.randomUUID().toString().replaceAll("-", "");//生成uuid避免文件名重复导致冲突覆盖
    filename=uuid+"_"+filename;
    upload.transferTo(new File(file,filename));
    return "forward:success.jsp";
}


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

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

Spring Boot 配置 IDEA和DevTools 热部署的方法

这篇文章主要介绍了Spring Boot 配置 IDEA和DevTools 热部署的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringBoot使用Redis缓存的实现方法

这篇文章主要介绍了SpringBoot使用Redis缓存的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringBoot中自定义参数绑定步骤详解

这篇文章主要介绍了SpringBoot中自定义参数绑定步骤详解,非常不错,具有参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Java实现abc字符串排列组合

这篇文章主要为大家详细介绍了JAVA实现abc字符串的排列组合,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java中后台线程实例解析

这篇文章主要介绍了Java中后台线程实例解析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

ehcache模糊批量移除缓存的方法

本篇文章主要介绍了ehcache模糊批量移除缓存的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java多线程join方法实例代码

这篇文章主要介绍了Java多线程join方法实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现字符串排列组合问题

这篇文章主要为大家详细介绍了java实现字符串排列组合问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java排列组合字符串的方法

这篇文章主要介绍了Java排列组合字符串的方法
收藏 0 赞 0 分享

Java语言中的自定义类加载器实例解析

这篇文章主要介绍了Java语言中的自定义类加载器实例解析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多