- 新建一个普通的maven工程
- 在pom.xml文件中引入相应的坐标
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<packaging>war</packaging> <name>springmvc_day02</name>
<groupId>zh.test.springmvc</groupId>
<artifactId>springmvc_day02</artifactId>
<version>1.0-SNAPSHOT</version> <!-- 版本锁定 -->
<properties>
<spring.version>5.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!--json字符串和Javabean对象互相转换的过程中,需要使用Jackson的jar包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<!--文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project> - 编写一个文件上传的jsp页面
<%--
Created by IntelliJ IDEA.
User: zhanghao
Date: 2019-08-25
Time: 17:05
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>文件上传</h3>
<form action="/fileupload.do" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload" /><br/>
<input type="submit" value="上传" />
</form>
</body>
</html> - 在resource的目录下新建一个spring.xml的配置文件,用 1 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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"> <!--如果<url-pattern>/</url-pattern>,任何资源都会拦截-->
<!--配置哪些资源不被拦截-->
<!--<mvc:resources mapping="/js/" location="/js/**" />-->
<!--<mvc:resources mapping="/css/" location="/css/**" />-->
<!--<mvc:resources mapping="/image/" location="/image/**" />--> <!--配置注解扫描-->
<context:component-scan base-package="zh.test"/>
<!--配置视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--跳转的页面的路径-->
<property name="prefix" value="/pages/" />
<!--跳转页面的后缀名称-->
<property name="suffix" value=".jsp" />
</bean> <!--配置文件上传的解析器组件。id的名称是固定,不能乱写-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--设置上传文件的总大小 8M = 8 * 1024 * 1024 -->
<property name="maxUploadSize" value="8388608" />
</bean> </beans> - 编写一个controller
package zh.test.demo2; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID; @Controller
public class UploadController { @RequestMapping(path="/fileupload.do")
public String upload(MultipartFile upload, HttpServletRequest request) throws IOException {
// 把文件上传到哪个位置
String realPath = request.getSession().getServletContext().getRealPath("/uploads");
// 创建该文件夹
File file = new File(realPath);
// 判断该文件夹是否存在
if(!file.exists()){
// 创建文件夹
file.mkdirs();
} // 获取到上传文件的名称
String filename = upload.getOriginalFilename(); // 把文件的名称修改成为一的值 sdfs-csdf-fwer-sdfw
String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
// 唯一的值
filename = uuid+"_"+filename;
System.out.println("文件名称:"+filename);
// 上传文件
upload.transferTo(new File(file,filename));
return "suc";
}
}
相关文章
- vue+spring boot实现文件上传
- 解析一个PHP木马,PHP文件上传安全检测组件
- spring cloud之利用zuul实现文件上传(十)
- Spring MVC 文件上传下载
- 【Feign】Spring Cloud Feign Client 调用文件上传接口(实现MultipartFile上传文件功能)
- 【已解决】Spring Cloud Feign 上传文件,提示:the request was rejected because no multipart boundary was found的问题
- spring-boot上传文件MultiPartFile获取不到文件问题解决
- spring配置文件中classpath路径解析
- Spring mvc 项目中页面访问不到静态文件,如img , js , css 等
- Spring MVC 监听文件上传进度,实现上传进度条