spring mvc:文件上传

在说文件上传之前,先来说一下会用到的类.

MultipartFile(文件的上传)--CommonsMultipartResolver 

SpringMVC 中,文件的上传,是通过 MultipartResolver 实现的。 所以,如果要实现文件的上传,只要在 spring-mvc.xml 中注册相应的 MultipartResolver 即可。

MultipartResolver 的实现类有两个:

  1. CommonsMultipartResolver
  2. StandardServletMultipartResolver

两个的区别:

  1. 第一个需要使用 Apache 的 commons-fileupload 等 jar 包支持,但它能在比较旧的 servlet 版本中使用。
  2. 第二个不需要第三方 jar 包支持,它使用 servlet 内置的上传功能,但是只能在 Servlet 3 以上的版本使用。

第一个使用步骤:

/*CommonsMultipartResolver  上传用到的两个包*/

"commons-fileupload:commons-fileupload:1.3.1",

"commons-io:commons-io:2.4"
<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver">



第二个:
<!--2 注册上传  StandardServletMultipartResolver
        第二个不需要第三方 jar 包支持,它使用 servlet 内置的上传功能,
        但是只能在 Servlet 3 以上的版本使用。
        -->

 

springmvc的@Validated注解使用

3. 在需要校验的pojo前边添加@Validated,在需要校验的pojo后边添加BindingResult bindingResult接收校验出错信息

注意:@Validated和BindingResult bindingResult是配对出现,并且形参顺序是固定的(一前一后)。

 

 

 

 

文件上传:

1.在xxx-servlet.xml文件中配置,spring mvc文件上传类bean配置

 <bean id="multipartResolver"   
 class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

2.新建一个FileModel类,继承org.springframework.web.multipart.MultipartFile文件上传

import org.springframework.web.multipart.MultipartFile;

public class FileModel {
   private MultipartFile file;

   public MultipartFile getFile() {
      return file;
   }

   public void setFile(MultipartFile file) {
      this.file = file;
   }
}

3.新建模板文件

file.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Spring MVC上传文件示例</title>
</head>
<body>
    <form:form method="POST" modelAttribute="fileUpload"
        enctype="multipart/form-data">
      请选择一个文件上传 : 
      <input type="file" name="file" />
        <input type="submit" value="提交上传" />
    </form:form>
</body>
</html>

  

上传结果页

file_result.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>Spring MVC上传文件示例</title>
</head>
<body>
    文件名称 :
    <b> ${fileName} </b> - 上传成功!
</body>
</html>

  

4.上传程序

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class FileUploadController {

   @Autowired
   ServletContext context; 

   @RequestMapping(value = "/fileUploadPage", method = RequestMethod.GET)
   public ModelAndView fileUploadPage() {
      FileModel file = new FileModel();
      ModelAndView modelAndView = new ModelAndView("fileUpload", "command", file);
      return modelAndView;
   }

   @RequestMapping(value="/fileUploadPage", method = RequestMethod.POST)
   public String fileUpload(@Validated FileModel file, BindingResult result, ModelMap model) throws IOException {
      if (result.hasErrors()) {
         System.out.println("validation errors");
         return "fileUploadPage";
      } else {            
         System.out.println("Fetching file");
         MultipartFile multipartFile = file.getFile();
         String uploadPath = context.getRealPath("") + File.separator + "temp" + File.separator;
         //Now do something with file...
         FileCopyUtils.copy(file.getFile().getBytes(), new File(uploadPath+file.getFile().getOriginalFilename()));
         String fileName = multipartFile.getOriginalFilename();
         model.addAttribute("fileName", fileName);
         return "success";
      }
   }
}

  

其中public ModelAndView fileUploadPage()代码块中的fileUpload跟file.jsp中的fileUpload是配套的:modelAttribute="fileUpload"

其中public String fileUpload(@Validated FileModel file, BindingResult result, ModelMap model)代码块中的@Validated 是用来验证数据的,验证数据跟BindingResult配套使用。FileModel file 是file.jsp中提交上来的file(fileUpload)

 

  

  

 

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值