SpringMVC上传文件实例
2016-05-08 00:00
639 查看
用Commons FileUpload 上传文件
首先来配置一下SpringMVC的 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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="Action" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <mvc:resources mapping="/css/**" location="/css/" /> <mvc:resources mapping="/*.html" location="/" /> <mvc:resources mapping="/image/**" location="/image/" /> <bean id="viewResolver" 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"> <property name="defaultEncoding" value="utf-8"/> <property name="maxUploadSize" value="10485760000"/> <property name="maxInMemorySize" value="40960"/> </bean> </beans>
写一下Controller类
/** * 视图控制类 * @author ZZQ * */ @Controller public class ScAction { /** * * @param file 页面传来的file 前面必须加注解@RequestParam * @param request HttpServletRequest对象,这里是为了获取路径 * @param model 可以把数据加载带栈顶,用EL表达式可以获取 * @return 目的视图 */ @RequestMapping("/save_file") public String saveFile(@RequestParam MultipartFile file, HttpServletRequest request, Model model ) { String fileName=file.getOriginalFilename(); File files = new File(request.getServletContext().getRealPath("/image"),fileName); if(!files.exists()){ files.mkdirs(); } try { //保存 file.transferTo(files); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } model.addAttribute("fileUrl", request.getContextPath()+"/image/"+fileName); return "savefile"; } /** * * @return 上传文件视图 */ @RequestMapping("/upload") public String upload(){ return "upload"; } }
两个jsp页面
upload.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'upload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="save_file" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="Submit" /></form> </body> </html>
savefile.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'upload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <img alt="" src="${fileUrl}"> </body> </html>
两个重要的jar
commons-fileupload-1.2.1.jarcommons-io-1.3.2.jar
相关文章推荐
- Struts2文件上传,以及上传拦截异常国际化处理
- Spring的四种事务管理(一种编程式事务+三种声明事务)<学习随笔>
- SpringMVC简单入门(注解)<学习随笔>
- java实现附件预览(openoffice+swftools+flexpaper)
- struts2<s:iterator>遍历map小结
- 判断Struts2的Get/Post请求
- 使用监听器实现Java Web的定时任务
- Spring DAO vs Spring ORM vs Spring JDBC
- Struts文件下载详解
- struts2入门(登陆实例)
- struts2文件上传,文件类型 allowedTypes对应
- 安卓异常java.lang.IllegalStateException: Content has been consumed
- Java 语言中 List、Set 和 Map 的区别
- javaWeb中文乱码问题
- Struts2注解开发
- 万能的Entry,两个变量的Model/JavaBean再也不用写了!
- springmvc 拦截器、国际化、验证
- SpringJUnit4(Spring,SpringMvc)
- volatile与可见性
- 跟着远叔学Spring系列(一)——Spring的由来及作用