1.需要在pom.xml中添加響應的依賴
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
2.上傳文件的接口
3.注意接收文件的參數對象,使用MultipartFile,若是多文件的上傳,那麼就可以使用List集合
要和再jsp文件中文件名相對應。
<form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
<input type="file" name="myfile">
<input type="text" name="description">
<input type="submit">
</form>
4.最後要在spring-mvc.xml中添加配置
<!-- 文件上傳配置 -->
<!-- 上傳文件大小限制,上限爲10MB,單位爲字節
上傳文件臨時路徑
-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="UTF-8"
p:maxUploadSize="5400000"
p:uploadTempDir="fileUpload/temp">
</bean>
項目地址: https://gitee.com/yuexingyingxue/springMvcTest1