示例【Spring MVC 文件上传】
导入文件上传相关jar包
创建User
package com.po;
import org.springframework.web.multipart.MultipartFile;
public class User {
private String userName;
private MultipartFile head;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public MultipartFile getHead() {
return head;
}
public void setHead(MultipartFile head) {
this.head = head;
}
}
创建UserController
package com.controller;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.po.User;
@Controller
public class UserController {
@RequestMapping(value="/registerForm")
public String registerForm(){
// 跳转到注册页面
return "registerForm";
}
@RequestMapping("/register")
public String register(HttpServletRequest request,@ModelAttribute
User user,Model model) throws IllegalStateException, IOException {
System.out.println(user.getUserName());
if(!user.getHead().isEmpty()){
// 上传文件路径
String path = request.getServletContext().getRealPath(
"/images");
// 上传文件名
String filename = user.getHead().getOriginalFilename();
File filepath = new File(path,filename);
// 判断路径是否存在,如果不存在就创建一个
if (!filepath.getParentFile().exists()) {
filepath.getParentFile().mkdirs();
}
// 将上传文件保存到一个目标文件当中
user.getHead().transferTo(new File(path+File.separator+ filename));
// 将用户添加到model
model.addAttribute("filename", user.getHead().getOriginalFilename());
System.out.println("上传文件路径:" + (path+File.separator+ filename));
return "success";
}else{
return "error";
}
}
}
创建registerForm.jsp
<form action="register" enctype="multipart/form-data" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="userName"></td>
</tr>
<tr>
<td>头像:</td>
<td><input type="file" name="head"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="注册"></td>
</tr>
</table>
</form>
创建success.jsp
<h3>注册成功</h3>
文件名:<a href="javascript:window.location.href = 'download?filename=' + encodeURIComponent( '${requestScope.filename }' )">
${requestScope.filename }</a>
</body>
创建error.jsp
<body>
<h3>注册失败</h3>
</body>
配置springmvc-config.xml
<!-- 使用注解驱动 -->
<mvc:annotation-driven />
<!-- 定义扫描的包 -->
<context:component-scan base-package="com.*" />
<!-- 静态资源处理 -->
<mvc:default-servlet-handler/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp" />
</bean>
<!-- 文件上传配置 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上传文件大小上限,单位为字节(10MB) -->
<property name="maxUploadSize">
<value>10485760</value>
</property>
<!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>
启动Tomcat并访问:http://localhost:8080/jackson/registerForm