SpringMVC----文件的上传(25)

1.Spring MVC 为文件上传提供了直接的支持,这种支持是通过即插即用的 MultipartResolver 实现的。Spring 用Jakarta Commons

    FileUpload 技术实现了一个MultipartResolver 实现类:CommonsMultipartResovler

   (MultipartResolver 是一个接口,spring默认的提供了两个实现类,我们使用CommonsMultipartResovler类来实现文件

       的上传,他其实是apache-commons-fileupload组件来完成工作的

2.Spring MVC 上下文中默认没有装配 MultipartResovler因此默认情况下不能处理文件的上传工作,如果想使用 Spring的文件上传功

   能,需现在上下文中配置 MultipartResolver

3.加入jar包

    --commons-fileupload-1.4.jar

    --commons-io-2.6.jar

4. 在spring.xml文件中配置MultipartResolver

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<!-- 配置扫描的包 -->
	<context:component-scan base-package="com.lishenhuan.springmvc.CRUD"></context:component-scan>

	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

    <mvc:default-servlet-handler/>

    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 配置MultipartResolver  -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
         <!-- 设置字符集的编码 -->
         <property name="defaultEncoding" value="UTF-8"></property>
          <property name="maxUploadSize"  value="1024000"></property>
    </bean>

</beans>

   5.jsp页面发送表单数据。

<form action="testFileUpload" method="post"  enctype="multipart/form-data">
        File:<input type="file" name="file">
        Desc:<input type="text" name="desc">
        <input  type="submit" name="Submit"> 
  </form>

 6.目标方法,获取上传的文件。@RequestParam获取请求中的参数。其中,使用MultipartFile类型的参数来接前台传来的

    文件,在MultipartFile类型的参数中,可以获取到上传文件的信息。

@RequestMapping("/testFileUpload")
public String testFileUpload(@RequestParam("desc") String desc,
			     @RequestParam("file") MultipartFile file) throws IOException {

	System.out.println("desc :" + desc );
	System.out.println("OriginalFilename :" + file.getOriginalFilename());
	System.out.println("InputStream : " + file.getInputStream());
	return "seccess";
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值