文件上传客户端三要素
- 表单项type=“file”
- 表单的提交方式是POST
- 表单的enctype属性是多部分表单形式,及enctype=“multipart/form-data”
单文件上传步骤
- 导入fileupload和id坐标
<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.6</version>
</dependency>
- 配置文件上传解析器 spring-mvc.xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="500000"/>
</bean>
- 编写文件上传代码
<form action="${pageContext.request.contextPath}/user/quick10" method="post" enctype="multipart/form-data">
名称<input type="text" name="username"><br>
文件<input type="file" name="upload">
<input type="submit" value="提交">
</form>
@RequestMapping("/quick10")
@ResponseBody
public void save10(String username, MultipartFile upload) throws IOException {
System.out.println(username);
System.out.println(upload);
String filename = upload.getOriginalFilename();
upload.transferTo(new File("G:\\upload\\"+filename));
}