SpringMVC—另一種方式文件上傳
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns=";
xmlns:xsi=";
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
;
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>commServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 手動配置加載的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/.xml</param-value>
</init-param>
<!-- 項目啟動時加載 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- servlet映射 -->
<servlet-mapping>
<!-- 所有.php結尾的請求都由commServlet管理 -->
<servlet-name>commServlet</servlet-name>
<url-pattern>.php</url-pattern>
</servlet-mapping>
<!-- 配置編碼過濾器 -->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 初始化參數,編碼是UTF-8 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!-- 過濾器映射 -->
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
servlet.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="
xmlns:xsi="
xmlns:context="
xmlns:mvc="
xmlns:util="
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util
;
<!-- 主鍵掃描,配置要掃描的Java類 -->
<context:component-scan base-package="com.cjh.action" />
<!-- 定義一個視圖解析類,基于ResourceView的解析器 ,此解析器是在url解析器上,加入了對jstl的支持-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前綴 -->
<property name="prefix" value="/jsp/" />
<!-- 后綴 -->
<property name="suffix" value=".jsp" />
</bean>
<!-- 處理文件上傳 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默認編碼 (ISO-8859-1) -->
<property name="defaultEncoding" value="utf-8" />
<!-- 最大內存大小 (10240)-->
<property name="maxInMemorySize" value="10240" />
<!-- 上傳后的臨時目錄名 (WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE) -->
<property name="uploadTempDir" value="/upload_temp/" />
<!-- 最大文件大小,-1為無限止(-1),注意maxUploadSize屬性的限制不是針對單個文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="-1" />
</bean>
<!-- SpringMVC在超出上傳文件限制時,會拋出org.springframework.web.multipart.MaxUploadSizeExceededException -->
<!-- 該異常是SpringMVC在檢查上傳的文件信息時拋出來的,而且此時還沒有進入到Controller方法中 -->
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- 遇到MaxUploadSizeExceededException異常時,自動跳轉到/jsp/fileError.jsp頁面 -->
<prop
key="org.springframework.web.multipart.MaxUploadSizeExceededException">
fileError
</prop>
</props>
</property>
</bean>
</beans>
action類:
package com.cjh.action;
import java.io.File;
import java.util.UUID;
import javax.servlet.ServletContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
/**
文件上傳類,實現ServletContextAware可以獲得servlet上下文
*/
@Controller
public class FileUploadAction implements ServletContextAware{
/setvlet上下文對象/
private ServletContext servletContext;
/提供對應的set方法,SpringMVC會自動的注入/
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
/**
- 文件上傳
- @param file
- @return
*/
@RequestMapping(value="/handleFormUpload",method=RequestMethod.POST)
public String fileUpload(@RequestParam("file")CommonsMultipartFile file){
/判斷文件是否為空,空直接返回上傳錯誤/
if(!file.isEmpty()){
/獲取本地文件存儲目錄/
String path = servletContext.getRealPath("/fileupload/");
/獲得文件名/
String fileName = file.getOriginalFilename();
/獲得文件后綴名/
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
/拼接文件路徑,以UUID生成文件名/
String filePath = path + File.separator + UUID.randomUUID().toString()+ suffix;
/創建一個要保存的新文件/
File saveFile = new File(filePath);
try{
/將文件寫入到新文件中/
file.getFileItem().write(saveFile);
}catch(Exception e ){
e.printStackTrace();
return "fileError";
}
return "fileUploadSuccess";
}else{
return "fileError";
}
}
}