以下示例顯示如何在使用Spring Web MVC框架的表單中上傳文件和處理。首先使用Eclipse IDE來創(chuàng)建一個(gè)WEB工程,實(shí)現(xiàn)一個(gè)上傳文件并保存的功能。并按照以下步驟使用Spring Web Framework開發(fā)基于動(dòng)態(tài)表單的Web應(yīng)用程序:
com.yiibai.springmvc
包下創(chuàng)建兩個(gè)Java類FileModel
, FileUploadController
。jsp
子文件夾下創(chuàng)建兩個(gè)視圖文件:fileUpload.jsp
和 success.jsp
。WebContent
文件夾下創(chuàng)建一個(gè)文件夾:temp
。CLASSPATH
中。完整的項(xiàng)目文件目錄結(jié)構(gòu)如下所示 -
FileModel.java 的代碼如下所示 -
package com.yiibai.springmvc;
import org.springframework.web.multipart.MultipartFile;
public class FileModel {
private MultipartFile file;
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
}
FileUploadController.java 的代碼如下所示 -
package com.yiibai.springmvc;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class FileUploadController {
@Autowired
ServletContext context;
@RequestMapping(value = "/fileUploadPage", method = RequestMethod.GET)
public ModelAndView fileUploadPage() {
FileModel file = new FileModel();
ModelAndView modelAndView = new ModelAndView("fileUpload", "command", file);
return modelAndView;
}
@RequestMapping(value="/fileUploadPage", method = RequestMethod.POST)
public String fileUpload(@Validated FileModel file, BindingResult result, ModelMap model) throws IOException {
if (result.hasErrors()) {
System.out.println("validation errors");
return "fileUploadPage";
} else {
System.out.println("Fetching file");
MultipartFile multipartFile = file.getFile();
String uploadPath = context.getRealPath("") + File.separator + "temp" + File.separator;
//Now do something with file...
FileCopyUtils.copy(file.getFile().getBytes(), new File(uploadPath+file.getFile().getOriginalFilename()));
String fileName = multipartFile.getOriginalFilename();
model.addAttribute("fileName", fileName);
return "success";
}
}
}
FileUpload-servlet.xml 的代碼如下所示 -
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<context:component-scan base-package="com.yiibai.springmvc" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
</beans>
這里的第一個(gè)服務(wù)方法fileUploadPage()
,我們已經(jīng)在名為“command
”的ModelAndView
對(duì)象中傳遞了一個(gè)空的FileModel
對(duì)象,因?yàn)槿绻鸍SP文件中使用<form:form>
標(biāo)簽,spring框架期望一個(gè)名稱為“command
”的對(duì)象。 因此,當(dāng)調(diào)用fileUploadPage()
方法時(shí),它返回fileUpload.jsp
視圖。
第二個(gè)服務(wù)方法fileUpload()
將根據(jù) URL => FileUpload/fileUploadPage
上的POST方法進(jìn)行調(diào)用。將根據(jù)提交的信息準(zhǔn)備要上傳的文件。最后從服務(wù)方法返回“success
”視圖,這將呈現(xiàn)success.jsp
視圖。
fileUpload.jsp 的代碼如下所示 -
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Spring MVC上傳文件示例</title>
</head>
<body>
<form:form method="POST" modelAttribute="fileUpload"
enctype="multipart/form-data">
請(qǐng)選擇一個(gè)文件上傳 :
<input type="file" name="file" />
<input type="submit" value="提交上傳" />
</form:form>
</body>
</html>
這里使用帶有value =“fileUpload”
的modelAttribute
屬性來映射文件用服務(wù)器模型上傳控件。
success.jsp 的代碼如下所示 -
<%@ page contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>Spring MVC上傳文件示例</title>
</head>
<body>
文件名稱 :
<b> ${fileName} </b> - 上傳成功!
</body>
</html>
完成創(chuàng)建源和配置文件后,發(fā)布應(yīng)用程序到Tomcat服務(wù)器。
現(xiàn)在啟動(dòng)Tomcat服務(wù)器,現(xiàn)在嘗試訪問URL => http://localhost:8080/FileUpload/fileUploadPage ,如果Spring Web應(yīng)用程序沒有問題,應(yīng)該看到以下結(jié)果:
提交所需信息后,點(diǎn)擊提交按鈕提交表單。 如果 Spring Web 應(yīng)用程序沒有問題,應(yīng)該看到以下結(jié)果: