1. 程式人生 > >Spring mvc 檔案的上傳與下載

Spring mvc 檔案的上傳與下載

一、檔案上傳

首先寫一個form表單,用來提交要上傳的檔案。程式碼如下:

<form method="post" action="<%=request.getContextPath()%>/file/fileUpLoad1.html" enctype="multipart/form-data">
	<input type="file" name="file"/>
	<input type="submit" value="上傳">
</form>

檔案上傳的表單中,必須要有enctype=multipart/form-data

後臺接收上傳的檔案有三種方式:

1、利用commons-fileupload. jar包中FileUtils類的copyInputStreamToFile方法進行檔案的讀寫

	@RequestMapping(value="/fileUpLoad1", method=RequestMethod.POST)
	public String fileUpLoad1(@RequestParam("file") MultipartFile file,
			HttpServletRequest request, ModelMap modelMap) {
		logger.info("***************************進入檔案上傳請求***********************");
		String viewName = "";
		try {
			if (file.isEmpty()) {//如果還沒有選擇檔案,則跳轉到提示頁面
				logger.info("***************************上傳檔案為空***********************");
				modelMap.put("errorMsg", "請選擇檔案後上傳");
				viewName = "error";
			}else{
				//檔案存放路勁,上傳的檔案放在專案的upload資料夾下
				String savePath = request.getSession().getServletContext().getRealPath("/upload");
				String realName = file.getOriginalFilename();
				//日誌列印上傳檔案的資訊
				logger.info("***************************[realName:"+realName+"]***********************");
				logger.info("***************************[fileName:"+file.getName()+"]***********************");
				logger.info("***************************[fileSize:"+file.getSize()/1024+"KB]***********************");
				logger.info("***************************[fileType:"+file.getContentType()+"]***********************");
				//將檔案資訊存放到modelMap中,在成功頁面讀取檔案資訊
				modelMap.put("fileName", realName);
				modelMap.put("fileSize", file.getSize()/1024+"KB");
				modelMap.put("fileType", file.getContentType());
				modelMap.put("filePath", savePath);
				//將上傳的檔案存放到upload下
				FileUtils.copyInputStreamToFile(file.getInputStream(), new File(savePath + File.separator + realName));
				logger.info("***************************上傳檔案為空***********************");
				viewName = "success";
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			logger.info("***************************檔案上傳失敗***********************");
			modelMap.put("errorMsg", "檔案上傳失敗");
			return "error";
		}
		return viewName;
	}

2、利用FileOutputStream和FileInputStream進行檔案的讀寫

	@RequestMapping(value="/fileUpLoad2", method=RequestMethod.POST)
	public String fileUpLoad2(@RequestParam("file") MultipartFile file, HttpServletRequest request, ModelMap modelMap){
		String viewName = "";
		OutputStream os = null;
		InputStream is = null;
		logger.info("***************************進入檔案上傳請求***********************");
		try{
			if (file.isEmpty()) {//如果還沒有選擇檔案,則跳轉到提示頁面
				logger.info("***************************上傳檔案為空***********************");
				modelMap.put("errorMsg", "請選擇檔案後上傳");
				viewName = "error";
			}else{
				//檔案存放路勁,上傳的檔案放在專案的upload資料夾下
				String savePath = request.getSession().getServletContext().getRealPath("/upload");
				String realName = file.getOriginalFilename();
				//日誌列印上傳檔案的資訊
				logger.info("***************************[realName:"+realName+"]***********************");
				logger.info("***************************[fileName:"+file.getName()+"]***********************");
				logger.info("***************************[fileSize:"+file.getSize()/1024+"KB]***********************");
				logger.info("***************************[fileType:"+file.getContentType()+"]***********************");
				//將檔案資訊存放到modelMap中,在成功頁面讀取檔案資訊
				modelMap.put("fileName", realName);
				modelMap.put("fileSize", file.getSize()/1024+"KB");
				modelMap.put("fileType", file.getContentType());
				modelMap.put("filePath", savePath);
				//將上傳的檔案存放到upload下
				os = new FileOutputStream(savePath+File.separator+realName);
				is = file.getInputStream();
				int len = 0;
				byte[] buffer = new byte[1024];
				while((len = is.read(buffer))!=-1){
					os.write(buffer,0,len);
				}
				logger.info("***************************上傳檔案為空***********************");
				viewName = "success";
			}
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			logger.info("***************************檔案上傳失敗***********************");
			modelMap.put("errorMsg", "檔案上傳失敗");
			return "error";
		}finally{
			if(os != null){
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(is != null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return viewName;
	}

3、利用spring mvc MultipartFile的Transto方法

	@RequestMapping(value="/fileUpLoad3", method=RequestMethod.POST)
	public String fileUpLoad3(@RequestParam("file") MultipartFile file, HttpServletRequest request, ModelMap modelMap){
		String viewName = "";
		logger.info("***************************進入檔案上傳請求***********************");
		try{
			if (file.isEmpty()) {//如果還沒有選擇檔案,則跳轉到提示頁面
				logger.info("***************************上傳檔案為空***********************");
				modelMap.put("errorMsg", "請選擇檔案後上傳");
				viewName = "error";
			}else{
				//檔案存放路勁,上傳的檔案放在專案的upload資料夾下
				String savePath = request.getSession().getServletContext().getRealPath("/upload");
				String realName = file.getOriginalFilename();
				//日誌列印上傳檔案的資訊
				logger.info("***************************[realName:"+realName+"]***********************");
				logger.info("***************************[fileName:"+file.getName()+"]***********************");
				logger.info("***************************[fileSize:"+file.getSize()/1024+"KB]***********************");
				logger.info("***************************[fileType:"+file.getContentType()+"]***********************");
				//將檔案資訊存放到modelMap中,在成功頁面讀取檔案資訊
				modelMap.put("fileName", realName);
				modelMap.put("fileSize", file.getSize()/1024+"KB");
				modelMap.put("fileType", file.getContentType());
				modelMap.put("filePath", savePath);
				//將上傳的檔案存放到upload下
				file.transferTo(new File(savePath + File.separator + realName));
				logger.info("***************************上傳檔案為空***********************");
				viewName = "success";
			}
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			logger.info("***************************檔案上傳失敗***********************");
			modelMap.put("errorMsg", "檔案上傳失敗");
			return "error";
		}
		return viewName;
	}


spring mvc 檔案上傳主要用到的是MultipartFile,用MultipartResolver解析器來解析,所以配置檔案中必須要有如下配置:

	<!-- 檔案上傳解析器 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 允許檔案上傳的最大位元組數,如果是多檔案上傳的話,表示的總檔案的大小 -->
		<property name="maxUploadSize" value="80000000"/>
		<!-- 檔案預設的編碼 -->
		<property name="defaultEncoding" value="UTF-8"/>
	</bean>
	<!-- 異常處理,當上傳的檔案超過multipartResolver中maxUploadSize的大小時,會丟擲異常,並由這個處理器處理,返貨到error_load頁面 -->
	<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_load</prop>
			</props>
		</property>
	</bean>
	

二、檔案下載

1、配置一個BeanNameViewResolver,將邏輯檢視名字解析為bean的Name屬性,從而根據name屬性,找定義View的bean

這種方式需要在控制Controller中返回一個bean的id或name,然後BeanNameViewResolver會根據根據id找到對應的bean解析 配置檔案:
	<!-- 檢視解析器,將邏輯檢視名字解析為bean的Name屬性,從而根據name屬性,找定義View的bean-->
	<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
		<property name="order" value="1"/>
	</bean>
	
其中,order表示viewResolver的優先順序,order的值越小,優先順序越高。根據優先順序來決定呼叫哪個解析器。
Controller控制器程式碼:
	/**
	 * 方法一:返回一個bean的Id,利用BeanNameViewResolver解析
	 * 
	 * @param modelMap
	 * @param srcPath
	 * @param fileName
	 * @param fileType
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/download1")
	public String download1(ModelMap modelMap,
			@RequestParam("path") String srcPath,
			@RequestParam("fileName") String fileName,
			@RequestParam("fileType") String fileType,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		fileName = URLDecoder.decode(fileName, "UTF-8");
		logger.info("**********************************開始下載檔案********************************");
		logger.info("**********************************下載請求path:" + srcPath+ "********************************");
		logger.info("**********************************下載請求fileName:" + fileName + "********************************");
		logger.info("**********************************下載請求fileType:" + fileType + "********************************");
		modelMap.put("path", srcPath);
		modelMap.put("fileName", fileName);
		modelMap.put("fileType", fileType);
		return "fileDownLoad";
	}

配置檔案中必須要有id或name=fileDownLoad的bean fileDownLoad的View
public class FileDownLoad extends AbstractView {

	@Override
	protected void renderMergedOutputModel(Map<String, Object> map,
			HttpServletRequest request, HttpServletResponse response) {
		OutputStream output = null;
		FileInputStream fileIn = null;
		String path = (String) map.get("path");
		String fileName = (String) map.get("fileName");
		String fileType = (String) map.get("fileType");
		try {
			response.setContentType(fileType + ";charset=UTF-8");
			output = response.getOutputStream();
			if (path == null || "".equals(path)) {
				output.write("檔案不存在".getBytes());
			} else {
				File file = new File(path + File.separator + fileName);
				// 當檔名中有中文時,會出現亂碼,通過new
				// String(fileName.getBytes("utf-8"),"ISO8859-1")轉換
				response.setHeader(
						"Content-Disposition",
						"attachment;filename="
								+ new String(fileName.getBytes("utf-8"),
										"ISO8859-1"));
				fileIn = new FileInputStream(file);
				int i = 0;
				byte[] buffer = new byte[1024];
				while ((i = fileIn.read(buffer)) != -1) {
					output.write(i);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fileIn != null) {
				try {
					fileIn.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (output != null) {
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

}


2.利用ResponseEntity<byte[]>

	/**
	 * 方法二:利用ResponseEntity<byte[]>
	 * 
	 * @param srcPath
	 * @param fileName
	 * @param fileType
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/download2")
	public ResponseEntity<byte[]> download2(
			@RequestParam("path") String srcPath,
			@RequestParam("fileName") String fileName,
			@RequestParam("fileType") String fileType) throws Exception {
		fileName = URLDecoder.decode(fileName, "UTF-8");
		logger.info("**********************************開始下載檔案********************************");
		logger.info("**********************************下載請求path:" + srcPath+ "********************************");
		logger.info("**********************************下載請求fileName:"+ fileName + "********************************");
		logger.info("**********************************下載請求fileType:"+ fileType + "********************************");
		File file = new File(srcPath + File.separator + fileName);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		// 當檔名中有中文時,會出現亂碼,通過new String(fileName.getBytes("utf-8"),"ISO8859-1")轉換
		headers.setContentDispositionFormData("attchement",
				new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
				headers, HttpStatus.CREATED);
	}


applicationContext.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<!-- 	<context:component-scan base-package="com.xliang.mvc"/>
	
	<mvc:annotation-driven/> -->
	
	<!-- spring mvc 3.1之後基於註解的處理器對映器HandlerMapping -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
	
	<!-- spring mvc 3.1之後基於註解的處理器介面卡HandlerAdapter -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
	
	<!-- 在spring mvc 3.1的RequestMappingHandlerAdapter中messageConverters屬性預設值是分別是:
		ByteArrayHttpMessageConverter,StringHttpMessageConverter,
		SourceHttpMessageConverter,AllEncompassingFormHttpMessageConverter
		如果這裡指定了其他的轉換器,則不會載入預設的轉換器。在使用ResponseEntity來下載檔案的時候,如果使用了其他的非預設的轉換器,
		則必須加上ByteArrayHttpMessageConverter,如下: -->
	<!-- 
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
			</list>
		</property>
	</bean> 
	-->
	
	<!-- 控制器 -->
	<bean id="fileUpAndDownloadController" class="com.xliang.mvc.controller.FileUpAndDownloadController"/>
	
	<bean id="fileDownloadController" class="com.xliang.mvc.controller.FileDownloadController"/>
	
	<!-- 檔案上傳解析器 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 允許檔案上傳的最大位元組數,如果是多檔案上傳的話,表示的總檔案的大小 -->
		<property name="maxUploadSize" value="80000000"/>
		<!-- 檔案預設的編碼 -->
		<property name="defaultEncoding" value="UTF-8"/>
	</bean>
	<!-- 異常處理,當上傳的檔案超過multipartResolver中maxUploadSize的大小時,會丟擲異常,並由這個處理器處理,返貨到error_load頁面 -->
	<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_load</prop>
			</props>
		</property>
	</bean>
	
	<!-- 檢視解析器,將邏輯檢視名字解析為一個路徑 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<property name="suffix" value=".jsp"/>
		<property name="order" value="2"/>
	</bean>
	
	<!-- 檢視解析器,將邏輯檢視名字解析為bean的Name屬性,從而根據name屬性,找定義View的bean-->
	<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
		<property name="order" value="1"/>
	</bean>
	
	<!-- 繼承AbstractView的bean BeanNameViewResolver會根據Controller中返回的-->
	<bean id="fileDownLoad" class="com.xliang.mvc.view.FileDownLoad"/>
</beans>



web.xml檔案的完整配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>springMVCDemo02</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<filter>
		<filter-name>CharacterEncoding</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<servlet>
		<servlet-name>FileUpload</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:conf/applicationContext.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>FileUpload</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>
</web-app>

完整程式碼請點選這裡下載