SpringMVC的初體驗-5

一. SpringMVC文件上傳

  1. 配置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"/> 單位KB——10M
	</bean>

</beans>
  1. 控制層
@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}
  1. 前端表單
  • 單文件上傳
<!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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章