使用MultipartResolver怎么实现一个文件上传功能

这篇文章将为大家详细讲解有关使用MultipartResolver怎么实现一个文件上传功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、网络空间、营销软件、网站建设、龙华网站维护、网站推广。

springMVC默认的解析器里面是没有加入对文件上传的解析的,,使用springmvc对文件上传的解析器来处理文件上传的时需要用springmvc提供的MultipartResolver的申明,又因为CommonsMultipartResolver实现了MultipartResolver接口,所以我们可以在springmvc配置文件中这样配置:

 <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>

 首先引入文件上传所需要的包,commons-logging-*.jar commons-io-*.jar  commons-fileupload-*.jar

新建一个JSP页面.

<%@ page language="java" contentType="text/html; charset=UTF-8" 
  pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>文件上传</title> 
</head> 
<body> 
  <%--<form action="user/fileUpload" method="post" enctype="multipart/form-data">--%> 
  <form action="user/fileUpload" method="post" enctype="multipart/form-data"> 
    <input type="file" name="fileUpload" /> 
    <input type="submit" value="上传" /> 
  </form> 
</body> 
</html>

springmvc上传文件的形式有很多,这里我介绍两种.

第一种,看Controller

package gd.hz.springmvc.controller; 
 
import java.io.File; 
import java.io.IOException; 
 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.multipart.commons.CommonsMultipartFile; 
import org.springframework.web.servlet.ModelAndView; 
 
@Controller("userController") 
@RequestMapping("user") 
public class UserController { 
 
  // 处理文件上传一 
  @RequestMapping(value = "fileUpload", method = RequestMethod.POST) 
  public ModelAndView fileUpload( 
      @RequestParam("fileUpload") CommonsMultipartFile file) { 
    // 获取文件类型 
    System.out.println(file.getContentType()); 
    // 获取文件大小 
    System.out.println(file.getSize()); 
    // 获取文件名称 
    System.out.println(file.getOriginalFilename()); 
 
    // 判断文件是否存在 
    if (!file.isEmpty()) { 
      String path = "D:/" + file.getOriginalFilename(); 
      File localFile = new File(path); 
      try { 
        file.transferTo(localFile); 
      } catch (IllegalStateException e) { 
        e.printStackTrace(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    } 
    return new ModelAndView("dataSuccess"); 
  } 
}

类CommonsMultipartFile为我们提供了许多对文件处理的方法.例如文件大小,上传文件名称,文件类型,具体用法可以查看spring的文档.transferTo就是将文件输出到指定地方. 

文件上传的第二种方法,这种方法比较常用:

package gd.hz.springmvc.controller; 
 
import java.io.File; 
import java.io.IOException; 
import java.util.Iterator; 
 
import javax.servlet.http.HttpServletRequest; 
 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.multipart.MultipartFile; 
import org.springframework.web.multipart.MultipartHttpServletRequest; 
import org.springframework.web.multipart.commons.CommonsMultipartResolver; 
 
@Controller("userController") 
@RequestMapping("user") 
public class UserController { 
 
  // 处理文件上传二 
  @RequestMapping(value = "fileUpload2", method = RequestMethod.POST) 
  public String fileUpload2(HttpServletRequest request) 
      throws IllegalStateException, IOException { 
    // 设置上下方文 
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( 
        request.getSession().getServletContext()); 
 
    // 检查form是否有enctype="multipart/form-data" 
    if (multipartResolver.isMultipart(request)) { 
      MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; 
 
      Iterator<String> iter = multiRequest.getFileNames(); 
      while (iter.hasNext()) { 
 
        // 由CommonsMultipartFile继承而来,拥有上面的方法. 
        MultipartFile file = multiRequest.getFile(iter.next()); 
        if (file != null) { 
          String fileName = "demoUpload" + file.getOriginalFilename(); 
          String path = "D:/" + fileName; 
 
          File localFile = new File(path); 
          file.transferTo(localFile); 
        } 
 
      } 
    } 
    return "dataSuccess"; 
  } 
}

关于使用MultipartResolver怎么实现一个文件上传功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

文章题目:使用MultipartResolver怎么实现一个文件上传功能
网页地址:https://www.cdcxhl.com/article48/ieoiep.html

成都网站建设公司_创新互联,为您提供企业建站域名注册面包屑导航网站内链微信小程序网站导航

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站优化排名