最近在研究springMVC,要想很好的应用前台框架,其中都逃不开要对文件上传的消化吸收,当然,springMVC
也不例外。下面就对springMVC的文件上传做一个解析,欢迎品读。
配置文件上传的解析器
<!-- 处理文件上传 -->
<!-- 通用的多部分解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置编码格式,默认是ISO-8859-1 -->
<property name="defaultEncoding" value="gbk"/>
<!-- 设置最大内存容量,当本地容量达到上限后持久化到数据库 -->
<property name="maxInMemorySize" value="10240"/>
<!-- 上传后的目录名,这里为本地临时路径。设置为默认将文件上传到upload文件夹下 -->
<property name="uploadTempDir" value="/upload/"/>
<!-- 上传最大文件大小,-1为无限制 -->
<property name="maxUploadSize" value="-1"/>
</bean></span>
配置操作类controller
<span style="font-family:SimSun;font-size:18px;">package com.mytest.controller;
import java.io.File;
import java.util.Date;
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;
@Controller
public class FileUploadController implements ServletContextAware{
private ServletContext servletContext;
public void setServletContext(ServletContext context){
this.servletContext=context;
}
/**
* 方法说明:文件上传
* 映射访问方法upload.do
* method=RequestMethod.POST,访问方式为post时才被执行
* @RequestParam("file") ,必须要写
* @param name 设置的文件名
* @param file 上传目标文件
* @return
*/
@RequestMapping(value="upload.do", method=RequestMethod.POST)
public String handleUploadFile(String name,@RequestParam("file") CommonsMultipartFile file){
if(!file.isEmpty()){//如果选中了上传目标文件
//获取本地存储路径,上传文件暂时保存到本地临时路径下,当内存容量达到上限后持久化到磁盘中
String path=this.servletContext.getRealPath("/tmp");
System.out.println(path);
//获取要上传目标文件的原来的文件名
String fileName=file.getOriginalFilename();
//获取上传目标文件的文件类型
String fileType=fileName.substring(fileName.lastIndexOf("."));
System.out.println(fileType);
//在临时路径下新建一个文件夹
File newFile=new File(path,new Date().getTime()+fileType);
try {
//将上传的文件写入新建的文件中
file.getFileItem().write(newFile);
} catch (Exception e) {
e.printStackTrace();
}
//重定向到上传成功页面
return "redirect:upload_ok.jsp";
}
else{
//重定向到上传失败页面
return "redirect:upload_error.jsp";
}
}
}</span>
文件上传入口
<span style="font-family:SimSun;font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>springmvc文件上传demo</title>
</head>
<body>
<form action="upload.do" method="post" enctype="multipart/form-data">
<input type="text" name="name"/>
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html></span>
上传成功跳转页面
<span style="font-family:SimSun;font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传</title>
</head>
<body>
<h1>恭喜您,文件上传成功!</h1>
</body>
</html>
</span>
失败跳转页面
<span style="font-family:SimSun;font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传</title>
</head>
<body>
<h1>很抱歉,文件上传失败!</h1>
</body>
</html></span>