Spring MVC文件上传下载
Spring MVC文件上传下载
单文件上传
底层是使用Apache fileupload 组件完成上传,Spring MVC对这种方式进行封装。
- pom.xml
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
jsp文件
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2023/2/13
Time: 13:13
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>
<%--上传文件的必要属性method="post" enctype="multipart/form-data"--%>
<form action="/file/upload" method="post" enctype="multipart/form-data">
<input type="file" name="img">
<input type="submit" value="上传">
</form>
</body>
</html>
1.input的type设置为file
2.form的method设置为post(设置为get则只能将文件名传给服务器)
3.from的enctype设置为multipart-form-data.(如果不设置也是只能将文件名传给服务器)