SpringMVC的初体验-5
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMVC的初体验-5相关的知识,希望对你有一定的参考价值。
一. SpringMVC文件上传
- 配置spring-mvc.xml
<?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.bee"/>
<!-- 视图解析器 -->
<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"/> 单位byte——10M
</bean>
</beans>
- 控制层
@Controller
public class FileUploadController
//单文件上传
@RequestMapping("/upload")
// <input type="file" name="file1"/>
public String uploadFile(@RequestParam("file1") MultipartFile file1,HttpServletRequest request)throws Exception
String filePath=request.getServletContext().getRealPath("/"); // “/”映射到了/WebContent/
System.out.println(filePath);
file1.transferTo(new File(filePath+"upload/"+file1.getOriginalFilename()));
return "redirect:success.html";
//多文件上传
@RequestMapping("/upload2")
public String uploadFiles(@RequestParam("file") MultipartFile[] files,HttpServletRequest request)throws Exception
String filePath=request.getServletContext().getRealPath("/"); // “/”映射到了/WebContent/
System.out.println(filePath);
for(MultipartFile file:files)
file.transferTo(new File(filePath+"upload/"+file.getOriginalFilename()));
return "redirect:success.html";
注意:WebContent/目录下的静态资源客户端浏览器是可以访问到的
http://localhost:8080/SpringMVCDemo/upload/a.jpg
^
|---> /WebContent/
|---> $pageContext.request.contextPath
- 前端表单
- 单文件上传
<!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>
- 多文件上传
<!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>
二. 文件上传涉及到的jar包
com.springsource.org.apache.commons.fileupload-1.2.0.jar
com.springsource.org.apache.commons.io-1.4.0.jar
以上是关于SpringMVC的初体验-5的主要内容,如果未能解决你的问题,请参考以下文章