- 创建项目
- 导入jar包
- 配置web.xml
1.web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
2.springmvc.xml配置,注意这里如果自定义位置,需要放在web-inf下,名称必须为servlet-name名称-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-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- 注解驱动 --> <mvc:annotation-driven/> <!-- 注解扫描 --> <context:component-scan base-package="com.springmvc"/> <!-- 视图控制器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/view/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 文件上次配置 id名称必须为 multipartResolver--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默认上传大小2M --> <property name="maxUploadSize" value="2097152"></property> </bean> </beans>
3.index.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="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" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="<%=basePath %>/upload/up.do" method="post" enctype="multipart/form-data"> <input type="file" name="inputName" > <input type="submit" value="upload"> </form> </body> </html>
4. SpringmvcUpload Action
package com.springmvc; import java.io.File; import java.io.IOException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; @Controller @RequestMapping(value="/upload/") public class SpringmvcUpload { @RequestMapping(value="up.do") public String inputUpload(@RequestParam(value="inputName",required=true) MultipartFile inputName){ try { inputName.transferTo(new File("F://"+inputName.getOriginalFilename())); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "redirect:/view/success.jsp"; } }
5.success.jsp 页面 ,放在view文件夹下
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 上传成功! </body> </html>
注意:
1.上传必须为POST
2.form 中必须有属性 enctype="multipart/form-data"
3.上次Action的方法中必须是
@RequestParam(value="inputName",required=true) MultipartFile mf
4.在spirngmv.xml中配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认上传大小2M -->
<property name="maxUploadSize" value="2097152"></property>
</bean>
这里的id名称必须是 multipartResolver
上传大小默认是2M,单位是字节。
时间: 2024-10-15 01:43:46