在写相关代码前,先配置环境
导入相关jar包,配置web.xml和applicationContext.xml
.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
配置applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--这里写spring的配置文件,主要配置和业务逻辑相关联的-->
<mvc:default-servlet-handler/>
<!--开启springmvc注解-->
<mvc:annotation-driven/>
<context:annotation-config/>
<!--扫描配置spring注解 要注意base-package="com.xiao",和自己创建的包名一致 -->
<context:component-scan base-package="com.xiao"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="102400000"></property>
</bean>
</beans>
1.写jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file"> <br>
<input type="file" name="file"> <br>
<input type="submit" value="上传" >
</form>
</body>
</html>
2.创建Upload类,该类要在com.xiao包下(原因看配置applicationContext.xml)
package com.xiao.controller;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Random;
@Controller
public class Upload {
// 上传
@RequestMapping("/upload")
public String uploading(MultipartFile file, HttpServletRequest request){
// 获取路径的文件名 要在web下创建upload文件夹
String realPath = request.getServletContext().getRealPath("/upload/");
// 获取文件的名字
String filename = file.getOriginalFilename();
// 给上传的文件改名字
String newName = new Date().getTime() + new Random().nextInt(9999) + filename;
// 实例化file
File f = new File(realPath + newName);
// 发送
try {
System.out.println(f);
file.transferTo(f);
} catch (IOException e) {
e.printStackTrace();
}
}
return "success"; //一个jsp页面
}
}
3.运行结果
4.在点击上传时可能会报找不到文件的错误
只需要在out\artifacts\_war_exploded下把创建的文件夹复制过去即可。
上传多个文件,用循环解决
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file"> <br>
<input type="file" name="file"> <br>
<input type="file" name="file"> <br>
<input type="submit" value="上传" >
</form>
</body>
</html>
-------------------------------------------------------------------------------------------
package com.xiao.controller;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Random;
@Controller
public class Upload {
// 上传
@RequestMapping("/upload")
public String uploading(MultipartFile file[], HttpServletRequest request){
// 获取路径的文件名
String realPath = request.getServletContext().getRealPath("/upload/");
for (int i=0;i<file.length;i++) {
// 获取文件的名字
String filename = file[i].getOriginalFilename();
// 给上传的文件改名字
String newName = new Date().getTime() + new Random().nextInt(9999) + filename;
// 实例化file
File f = new File(realPath + newName);
// 发送
try {
System.out.println(f);
file[i].transferTo(f);
} catch (IOException e) {
e.printStackTrace();
}
}
return "success";
}
}