SpringMVC上传文件实例

 用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.jar

commons-io-1.3.2.jar


你可能感兴趣的:(spring,spring,mvc,上传)