springmvc.实现文件上传|学习笔记

简介: 快速学习springmvc.实现文件上传

开发者学堂课程【SpringMVC框架入门:springmvc.实现文件上传】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/22


springmvc.实现文件上传

目录:

一.通过commons-fileupload.来实现。

二.配置springmvc的配置解析器

三.jsp页面

四.Controller 代码

五.批量上传的代码

1.通过commons-fileupload.来实现。

导入相关jar包:commons-fileupload,commons-io;

2. 配置springmvc的配置解析器。

<bean id= "multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name= "DefaultEncoding"
value="utf-8"></property>
<property name= "maxUploadSize"
value="10485760000"></property>
<property name="maxInMemorySize"
value="40960"></property>
</bean>
3.jsp页面

<form action= "upload.do"method="post"
enctype="multipart/form-data">

file:<input type="file"name="file"/<input type="submit"value="上传"/>
</ form>
</body>

4.Controller 代码

@Controller
public class FileuploadController, {
@ReguestMapping("/upload")
public String
fileupload(@RequestParam("file")?ommonsMultipartFilefile,HttpServletRequest,reg) throws I0Exception{
//获取文件名
//file,get0riginalEilename();
//获取上传文件的路径
String path =reg. getRealPath("/fileueload");

InputStream is= file. getInputStream();
OutputStream os = new FileOutputStream (new
File(path,file.get0riginalFilename()));
int,len=0;
byte[] buffer = new byte[400];
while((len=is,read(buffer))!=-1)
os.write(buffer,0,len);
os.close();
is.close();
return "/index.isp" ;
}

注释:RequestParam 这个是重点;

5.批量上传的代码

@ReauestMapping("/batch")

public String

fileupload(@Regues.tParam("file")CommonsMultipartFile

file[] ,HttpServletReguest reg) throws IOException{

//获取文件名
//file.getrisinalFilename();
//获取上传文件的路径
String path = reg, getRealPathf"(/fileupload");

for(int i=0;i<file.length;i++){
InputStream is = file[i] . getInputStream();

OutputStream os= new FileOutputStream(new
File(path,file[i].getOriginalFilename()));
int len=0; ,
byte[] buffer = new byte[400];
while( (len=is.read(buffer))!=-1)
os..write(buffer, 0, len);
os.close();
is.close();
return"/index.isp";

}

相关文章
|
2天前
|
云安全 人工智能 自然语言处理
AI说的每一句话,都靠谱吗?
阿里云提供AI全栈安全能力,其中针对AI输入与输出环节的安全合规挑战,我们构建了“开箱即用”与“按需增强”相结合的多层次、可配置的内容安全机制。
|
6天前
|
存储 人工智能 安全
AI 越智能,数据越危险?
阿里云提供AI全栈安全能力,为客户构建全链路数据保护体系,让企业敢用、能用、放心用
|
9天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
3天前
|
消息中间件 安全 NoSQL
阿里云通过中国信通院首批安全可信中间件评估
近日,由中国信通院主办的 2025(第五届)数字化转型发展大会在京举行。会上,“阿里云应用服务器软件 AliEE”、“消息队列软件 RocketMQ”、“云数据库 Tair”三款产品成功通过中国信通院“安全可信中间件”系列评估,成为首批获此认证的中间件产品。此次评估覆盖安全可信要求、功能完备性、安全防护能力、性能表现、可靠性与可维护性等核心指标,标志着阿里云中间件产品在多架构适配与安全能力上达到行业领先水平。
302 192
|
3天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
352 166
|
2天前
|
开发者
「玩透ESA」ESA启用和加速-ER在加速场景中的应用
本文介绍三种配置方法:通过“A鉴权”模板创建函数并设置触发器路由;在ESA上配置回源302跟随;以及自定义响应头。每步均配有详细截图指引,帮助开发者快速完成相关功能设置,提升服务安全性与灵活性。
303 2
|
8天前
|
数据采集 人工智能 自然语言处理
3分钟采集134篇AI文章!深度解析如何通过云无影AgentBay实现25倍并发 + LlamaIndex智能推荐
结合阿里云无影 AgentBay 云端并发采集与 LlamaIndex 智能分析,3分钟高效抓取134篇 AI Agent 文章,实现 AI 推荐、智能问答与知识沉淀,打造从数据获取到价值提炼的完整闭环。
457 93