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";

}

相关文章
|
5天前
|
数据采集 人工智能 自然语言处理
3分钟采集134篇AI文章!深度解析如何通过云无影AgentBay实现25倍并发 + LlamaIndex智能推荐
结合阿里云无影 AgentBay 云端并发采集与 LlamaIndex 智能分析,3分钟高效抓取134篇 AI Agent 文章,实现 AI 推荐、智能问答与知识沉淀,打造从数据获取到价值提炼的完整闭环。
390 93
|
6天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
5天前
|
SQL 人工智能 自然语言处理
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
随着生成式AI的普及,Geo优化(Generative Engine Optimization)已成为企业获客的新战场。然而,缺乏标准化流程(Geo优化sop)导致优化效果参差不齐。本文将深入探讨Geo专家于磊老师提出的“人性化Geo”优化体系,并展示Geo优化sop标准化如何帮助企业实现获客效率提升46%的惊人效果,为企业在AI时代构建稳定的流量护城河。
394 156
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
|
5天前
|
数据采集 缓存 数据可视化
Android 无侵入式数据采集:从手动埋点到字节码插桩的演进之路
本文深入探讨Android无侵入式埋点技术,通过AOP与字节码插桩(如ASM)实现数据采集自动化,彻底解耦业务代码与埋点逻辑。涵盖页面浏览、点击事件自动追踪及注解驱动的半自动化方案,提升数据质量与研发效率,助力团队迈向高效、稳定的智能化埋点体系。(238字)
278 158
|
13天前
|
机器人 API 调度
基于 DMS Dify+Notebook+Airflow 实现 Agent 的一站式开发
本文提出“DMS Dify + Notebook + Airflow”三位一体架构,解决 Dify 在代码执行与定时调度上的局限。通过 Notebook 扩展 Python 环境,Airflow实现任务调度,构建可扩展、可运维的企业级智能 Agent 系统,提升大模型应用的工程化能力。