当前位置: 移动技术网 > IT编程>开发语言>Java > SpringMVC上传文件的两种方法

SpringMVC上传文件的两种方法

2019年07月19日  | 移动技术网IT编程  | 我要评论
在该示例中,阐述了springmvc如何上传文件。 1、上传页面upload.jsp <body> <form action="/

在该示例中,阐述了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://www.lhsxpumps.com/_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); 
      } 
    } 
  } 

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

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网