基于表单的文件上传
标签<input type=”file”/>会在浏览器中显示一个输入框和一个按钮,输入框可供用户填写本地文件的文件名和路径名,按钮可以让浏览器打开一个文件选择框供用户选择文件。
文件上传的表单例子如下:
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="myfile"/>
...
</form>
对于基于表单的文件上传,不要忘记使用enctype属性,并将它的值设置为multipart/form-data,同时将表单的提交方式设置为post。为什么要这样呢?下面从enctype的属性说起。
表单的enctype属性指定的是表单数据的编码方式,该属性有以下3个值。
- application/x-www-from-urlencoded:这是默认的编码方式,它只处理表单域李的value属性值。
- multipart/form-data:该编码方式以二进制流的方式来处理表单数据,并将文件域指定文件的内容封装到请求参数里。
- Text/plain:该编码方式只有当表单的action属性以mailto:URL的形式是才使用,主要适用于直接通过表单发送邮件的方式。
由上面的三个属性的解释可知,在基于表单上传文件时enctype的属性值应为multipart/form-data。
单文件上传关键步骤如下:
创建文件选择页面
在WebContent目录下创建JSP页面oneFile.jsp,在该页面中使用表单上传单个文件,具体代码如下:
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/onefileupload" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="mmyfile"><br>
文件描述:<input type="text" name="description"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
创建POJO类
在src目录下创建pojo包,在该包中创建POJO类FileDomain。然后在该POJO类中声明一个MultipartFile类型属性封装被上传的文件信息,属性名与文件选择页面oneFile.jsp中的file类型的表单参数名myfile相同。具体代码如下:
package pojo;
import org.springframework.web.multipart.MultipartFile;
public class FileDomain {
private String description;
private MultipartFile mmyfile;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public MultipartFile getMmyfile() {
return mmyfile;
}
public void setMmyfile(MultipartFile mmyfile) {
this.mmyfile = mmyfile;
}
}
创建控制器类
在src目录下创建controller包,并在该包中创建FileUploadController控制器类。具体代码如下:
package controller;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import pojo.FileDomain;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
@Controller
public class FileUploadController {
private static final Log logger= LogFactory.getLog(FileUploadController.class);
@RequestMapping("/onefilejsp")
public String file(){
return "oneFile";
}
@RequestMapping("/onefileupload")
public String oneFileUpload(@ModelAttribute FileDomain fileDomain, HttpServletRequest request){
String realpath=request.getServletContext().getRealPath("uploadfiles");
System.out.println(realpath);
String fileName=fileDomain.getMmyfile().getOriginalFilename();
File targetFile=new File(realpath,fileName);
if(!targetFile.exists()){
targetFile.mkdirs();
}
try {
fileDomain.getMmyfile().transferTo(targetFile);
logger.info("成功");
}
catch(Exception e){
e.printStackTrace();
}
return "showOne";
}
}
创建配置文件
在上传文件时需要在配置文件中使用Spring的CommonsMultipartResolver类配置MultipartResolver用于文件上传,应用的配置文件springmvc-servlet.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="controller"></context:component-scan>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="5400000"/>
</bean>
</beans>