05.spring mvc FileUpload单文件上传

一、说明

spring mvc提供了用于文件上传的公共服务,只需要简单配置 ,就可以使用spring mvc提供的接口完成文件上传。

二、使用

1、在spring mvc配置文件(mvc-dispatcher-servlet.xml)中,增加spring mvc提供的专门用来解析上传的文件的bean

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	<!--限制文件最大为200M-->
    <property name="maxUploadSize" value="209715200" />
    <!--文件编码为UTF-8-->
	<property name="defaultEncoding" value="UTF-8" />
    <!--延迟文件解析,需要时才解析-->
	<property name="resolveLazily" value="true" />
</bean>

此bean需要项目增加如下依赖:

<!--FILE UPLOAD需要依赖的apache包-->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

2、增加跳转到操作上传JSP页面的controller

@RequestMapping(value="/upload",method = RequestMethod.GET)
    public String showUploadPage(){
        return "course_admin/file";
    }

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

<link rel="stylesheet" href="<%=request.getContextPath()%>/resources/css/main.css" type="text/css" />
</head>
<body>
<div align="center">

<h1>上传附件</h1>
<form method="post" action="/courses/doUpload" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
</div>
</body>
</html>
  • 其中form表单是提交上传文件的
  • 表单提交时访问的URL是:/courses/doUpload
  • 表单enctype属性是文件上传时表单必须要设置的一个属性,否则无法进行文件上传

4、doUpload controller method的内容

 /**
   * RequestParam注解使得controller方法参数和一个表单元素相关联,这里的file对应jsp页面中:<input type="file" name="file"/>的name值
   * @param multipartFile
   * @return
   */
  @RequestMapping(value="/doUpload",method = RequestMethod.POST)
  public String doUploadFile(@RequestParam("file") MultipartFile multipartFile){
      //服务器端获取到文件后,将文件进行存储,供其他业务使用
        if(!multipartFile.isEmpty()){
            log.debug("Process file: {} ",multipartFile.getOriginalFilename());
            //这里只将文件保存到一个临时目录
            FileUtils.copyInputStreamToFile(multipartFile.getInputStream(),new File("E:\\01.study\\04.springmvc\\workspace\\springmvc\\temp",System.currentTimeMillis()+"_"+multipartFile.getOriginalFilename()));
        }
        return "success";
  }
  • @RequestParam,@RequestParam("xx") 表示在前端传递过来的参数中必须有个参数名称为“xx”(可以使用required=false避免必须)
  • @ModelAttribute,@ModelAttribute("xx") 表示将前端传递过来的参数按照名称注入到对应的对象中,“xx”只是表示放到ModelMap中的key值 或者对应注解修饰对象的属性名

5、上传成功页面success.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>
<link rel="stylesheet" href="<%=request.getContextPath()%>/resources/css/main.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<h1>Success</h1>
</div>
</body>
</html>

6、测试

启动web容器(工程中使用的jetty,也可以配置tomcat)

启动浏览器,输入如下网址:http://localhost:8080/courses/upload

选择一个文件进行上传:

日志输出:

存储到目录下文件

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值