J2EE系列之SpringMVC学习笔记(六)--文件上传

回看Struts2的文件上传博客,struts2文件上传使用的是fileupload组件。SpringMvc不同,下面通过实例看一下SpringMvc的文件上传功能。

一、单文件上传

1.新建工程SpringMVC04,按照SpringMVC01的工程配置好。

2.SpringMVC 要想支持文件上传的话,spring-mvc.xml文件中要添加一个bean:

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        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.xsd">

	<!-- 使用注解的包,包括子集 -->
    <context:component-scan base-package="com.test"/>

    <!-- 视图解析器 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		
		<property name="defaultEncoding" value="UTF-8"/>  
	    <property name="maxUploadSize" value="10000000"/>

	</bean>

</beans>
新加入的这个bean中有两个配置。一个是默认编码采用UTF-8,一个是设置上传的文件最大为10M。

3.要想使用这个bean,工程中还得添加两个jar包:


4.新建控制层FileUploadController:

package com.test.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FileUploadController {

	@RequestMapping("/upload")
	public String uploadFile(@RequestParam("file1") MultipartFile file1,HttpServletRequest request)throws Exception{
		String filePath=request.getServletContext().getRealPath("/");
		System.out.println(filePath);
		file1.transferTo(new File(filePath+"upload/"+file1.getOriginalFilename()));
		return "redirect:success.html";
	}
	
}

这里使用了@RequestParam("file1")注解来获取前台传入的文件,这里参数名为file1,所以前台页面中要有一个name属性值为file1的上传文件控件。request形参用来获取文件上传路径。

函数中request.getServletContext().getRealPath("/");来获取项目路径。file1.transferTo()可以直接把文件输出到一个目录中,我们这里上传到了filePath+"upload/"+file1.getOriginalFilename(),这里上传到了工程目录下upload文件夹下,文件名还是原来的名字。最后重定向到success.html页面。

5.在工程WebContent目录下新建文件夹upload。

6.新建success.html页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
上传成功!
</body>
</html>

7.新建index.jsp页面,用来上传文件:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="upload.do" method="post" enctype="multipart/form-data">
		<table>
			<tr>
				<th colspan="2">上传文件</th>
			</tr>
			<tr>
				<td>文件一</td>
				<td><input type="file" name="file1"/></td>
			</tr>
			<tr>
				<td colspan="2">
					<input type="submit" value="上传文件"/>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>
这里注意上传文件控件的name属性值为file1.
8.运行程序:


这里选择了一张图片,并点击上传文件按钮:


我们请求一下这个图片:


可以看到文件上传成功了。这里在upload文件夹下取到了上传的图片。查看一下工程中upload文件夹,里面却没有图片。看一下控制台输出:


这个是我们在控制层方法中输入的工程路径。在开发环境中,tomcat是作为eclipse的一个插件的,这里为插件的目录,所以文件上传到了插件的目录中,但是并不影响我们取文件。在正式开发环境中也是没有影响的。


二、多文件上传

1.修改控制层,添加一个新的处理方法:

@RequestMapping("/upload2")
	public String uploadFiles(@RequestParam("file") MultipartFile[] files,HttpServletRequest request)throws Exception{
		String filePath=request.getServletContext().getRealPath("/");
		System.out.println(filePath);
		for(MultipartFile file:files ){
			
			file.transferTo(new File(filePath+"upload/"+file.getOriginalFilename()));
		}
		return "redirect:success.html";
	}

这里使用一个数组来接收上传的多个文件。使用@RequestParam("file")来获取上传的文件,这里前台中上传文件的控件name属性值为file。

2.修改index.jsp页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="upload2.do" method="post" enctype="multipart/form-data">
		<table>
			<tr>
				<th colspan="2">上传文件</th>
			</tr>
			<tr>
				<td>文件一</td>
				<td><input type="file" name="file"/></td>
			</tr>
			<tr>
				<td>文件二</td>
				<td><input type="file" name="file"/></td>
			</tr> 
			<tr>
				<td colspan="2">
					<input type="submit" value="上传文件"/>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>
这里两个文件上传控件的name属性值都是file。


3.运行程序:


这里选择两个文件,点击上传文件按钮,跳转到success.html页面。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值