资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

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

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

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

  
     
     
     
  

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

新建一个JSP页面.

<%@ page language="java" contentType="text/html; charset=UTF-8" 
  pageEncoding="UTF-8"%> 
 
 
 
 
文件上传 
 
 
  <%----%> 
   
     
     
   
 

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 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怎么实现一个文件上传功能
URL标题:http://cdkjz.cn/article/ieoiep.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220