以下示例显示如何在使用spring web mvc框架的表单中上传文件和处理。首先使用eclipse ide来创建一个web工程,实现一个上传文件并保存的功能。并按照以下步骤使用spring web framework开发基于动态表单的web应用程序:
com.h3.springmvc
包下创建两个java类filemodel
, fileuploadcontroller
。jsp
子文件夹下创建两个视图文件:fileupload.jsp
和 success.jsp
。webcontent
文件夹下创建一个文件夹:temp
。classpath
中。完整的项目文件目录结构如下所示 -
filemodel.java 的代码如下所示 -
package com.h3.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.h3.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.h3.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>
这里的第一个服务方法fileuploadpage()
,我们已经在名为“command
”的modelandview
对象中传递了一个空的filemodel
对象,因为如果jsp文件中使用<form:form>
标签,spring框架期望一个名称为“command
”的对象。 因此,当调用fileuploadpage()
方法时,它返回fileupload.jsp
视图。
第二个服务方法fileupload()
将根据 url => fileupload/fileuploadpage
上的post方法进行调用。将根据提交的信息准备要上传的文件。最后从服务方法返回“success
”视图,这将呈现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">
请选择一个文件上传 :
<input type="file" name="file" />
<input type="submit" value="提交上传" />
</form:form>
</body>
</html>
这里使用带有value =“fileupload”
的modelattribute
属性来映射文件用服务器模型上传控件。
success.jsp 的代码如下所示 -
<%@ page contenttype="text/html; charset=utf-8"%>
<html>
<head>
<title>spring mvc上传文件示例</title>
</head>
<body>
文件名称 :
<b> ${filename} </b> - 上传成功!
</body>
</html>
完成创建源和配置文件后,发布应用程序到tomcat服务器。
现在启动tomcat服务器,现在尝试访问url => http://localhost:8080/fileupload/fileuploadpage ,如果spring web应用程序没有问题,应该看到以下结果:
提交所需信息后,点击提交按钮提交表单。 如果 spring web 应用程序没有问题,应该看到以下结果: