JAVA全系列 教程
3762个小节阅读:7095.5k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
上传是Web工程中很常见的功能,SpringMVC框架简化了文件上传的代码,我们首先使用JAVAEE原生方式上传文件:
创建新的SpringMVC项目,在web.xml中将项目从2.3改为3.1,即可默认开启el表达式
xxxxxxxxxx
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
编写上传表单
xxxxxxxxxx
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>上传</title>
</head>
<body>
<h3>文件上传</h3>
<%-- 上传表单的提交方式必须是post --%>
<%-- enctype属性为multipart/form-data,意思是不对表单数据进行编码 --%>
<form action="/fileUpload" method="post" enctype="multipart/form-data">
<%-- 文件选择控件,类型是file,必须要有name属性--%>
选择文件:<input type="file" name="upload"/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
接收请求体数据:
xxxxxxxxxx
@RequestMapping("/fileUpload")
public String upload(HttpServletRequest request) throws Exception {
// 获取输入流
ServletInputStream is = request.getInputStream();
// 从输入流获取请求体数据
int i = 0;
while ((i=is.read())!=-1){
System.out.println((char)i);
}
return "index";
}