SpringMVC上传文件的两种方法

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

在该示例中,阐述了SpringMVC如何上传文件。
1、上传页面upload.jsp

<body> 
  <form action="/TestSpringMVC3/data/uploadfile" enctype="multipart/form-data" method="post"> 
    file:<input type="file" name="file"><br> 
    <input type="submit" value="upload file"> 
  </form> 
</body> 

2、controller配置文件

<?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:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/context  
      http://www.springframework.org/schema/context/spring-context-3.0.xsd 
      http://www.springframework.org/schema/aop  
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
      http://www.springframework.org/schema/tx  
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
      http://www.springframework.org/schema/context  
      http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
  <!-- 
    使Spring支持自动检测组件,如注解的Controller 
  --> 
  <context:component-scan base-package="cn.com.yy.controller"/> 
   
  <!-- 开启注解配置 --> 
  <mvc:annotation-driven/> 
     
  <!-- 支持JSP JSTL的解析器 --> 
  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/page/"/> 
    <property name="suffix" value=".jsp"/> 
   </bean> 
    
   <!-- 配置文件上传解析器 --> 
   <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    <property name="defaultEncoding" value="utf-8"/> 
    <property name="maxUploadSize" value="10485760000"/> 
    <property name="maxInMemorySize" value="40960"/> 
   </bean> 
</beans> 

主要是添加了文件上传的解析器,配置了默认编码,最大的上传大小以及缓存大小等参数。

3、Controller

package cn.com.yy.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 org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.multipart.commons.CommonsMultipartFile; 
 
@Controller 
@RequestMapping("/data") 
public class FileUploadController { 
   
  /** 
   * method1:通过参数CommonsMultipartFile进行解析 
   * @RequestParam("file")中的file对应于upload.jsp中的file类型的name对应的名称 
   * @param file 
   * @return 
   * @throws IOException 
   */ 
  @RequestMapping(value="/uploadfile") 
  public String upload1(@RequestParam("file") CommonsMultipartFile file) throws IOException{ 
    //获取文件名称 
    String fileName = file.getOriginalFilename(); 
    //写入本地磁盘 
    InputStream is = file.getInputStream(); 
    byte[] bs = new byte[1024]; 
    int len; 
    OutputStream os = new FileOutputStream(new File("D:/temp/" + fileName)); 
    while ((len = is.read(bs)) != -1) { 
      os.write(bs, 0, len); 
    } 
    os.close(); 
    is.close(); 
    return "upload_success"; 
  } 
   
  @RequestMapping("/upload") 
  public String toPage(){ 
    return "upload"; 
  } 
} 

4、返回页面upload_success.jsp

<body> 
  upload file success!! 
</body> 

5、测试

访问  http://localhost:8080/TestSpringMVC3/data/upload  跳转到上传页面

    

选择文件上传

                    

点击上传会跳转到上传成功页面。

上述方法只是简单的讲解了SpringMVC如何上传文件。    

第二种方法:使用SpringMVC封装的方法进行文件上传

/** 
   * 使用SpringMVC封装好的方法进行文件上传 
   * @param request 
   * @param response 
   * @throws IllegalStateException 
   * @throws IOException 
   */ 
  @RequestMapping("/uploadfile2") 
  public void upload2(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException{ 
    //获取解析器 
    CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext()); 
    //判断是否是文件 
    if(resolver.isMultipart(request)){ 
      //进行转换 
      MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)(request); 
      //获取所有文件名称 
      Iterator<String> it = multiRequest.getFileNames(); 
      while(it.hasNext()){ 
        //根据文件名称取文件 
        MultipartFile file = multiRequest.getFile(it.next()); 
        String fileName = file.getOriginalFilename(); 
        String localPath = "D:/temp/" + fileName; 
        File newFile = new File(localPath); 
        //上传的文件写入到指定的文件中 
        file.transferTo(newFile); 
      } 
    } 
  } 

该方法上传文件效率更优。

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

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

Java数据类型的规则

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

Spring整合TimerTask实现定时任务调度

这篇文章主要介绍了Spring整合TimerTask实现定时任务调度的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解SpringMVC使用MultipartFile实现文件的上传

本篇文章主要介绍了SpringMVC使用MultipartFile实现文件的上传,本地的文件上传到资源服务器上,比较好的办法就是通过ftp上传。这里是结合SpringMVC+ftp的形式上传的,有兴趣的可以了解一下。
收藏 0 赞 0 分享

SpringMVC上传文件的三种实现方式

本篇文章主要介绍了SpringMVC上传文件的三种实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例

本篇文章主要介绍了微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

浅析Java中的继承与组合

本文将介绍组合和继承的概念及区别,并从多方面分析在写代码时如何进行选择。文中通过示例代码介绍的很详细,有需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

利用反射获取Java类中的静态变量名及变量值的简单实例

下面小编就为大家带来一篇利用反射获取Java类中的静态变量名及变量值的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java启动线程的3种方式对比分析

这篇文章主要为大家对比分析了java启动线程的3种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringMVC上传和解析Excel方法

这篇文章主要介绍了SpringMVC上传和解析Excel方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JAVA中String类与StringBuffer类的区别

这篇文章主要为大家详细介绍了JAVA中String类与StringBuffer类的区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多