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

}

相关文章
|
3天前
|
人工智能 运维 安全
|
5天前
|
SpringCloudAlibaba 负载均衡 Dubbo
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
本文对比分析了SpringCloudAlibaba框架下Feign与Dubbo的服务调用性能及差异。Feign基于HTTP协议,使用简单,适合轻量级微服务架构;Dubbo采用RPC通信,性能更优,支持丰富的服务治理功能。通过实际测试,Dubbo在调用性能、负载均衡和服务发现方面表现更出色。两者各有适用场景,可根据项目需求灵活选择。
395 124
微服务架构下Feign和Dubbo的性能大比拼,到底鹿死谁手?
|
8天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
744 109
|
2天前
|
算法 Python
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
【轴承故障诊断】一种用于轴承故障诊断的稀疏贝叶斯学习(SBL),两种群稀疏学习算法来提取故障脉冲,第一种仅利用故障脉冲的群稀疏性,第二种则利用故障脉冲的额外周期性行为(Matlab代码实现)
229 152
|
4天前
|
Java 数据库 数据安全/隐私保护
Spring 微服务和多租户:处理多个客户端
本文介绍了如何在 Spring Boot 微服务架构中实现多租户。多租户允许单个应用实例为多个客户提供独立服务,尤其适用于 SaaS 应用。文章探讨了多租户的类型、优势与挑战,并详细说明了如何通过 Spring Boot 的灵活配置实现租户隔离、动态租户管理及数据源路由,同时确保数据安全与系统可扩展性。结合微服务的优势,开发者可以构建高效、可维护的多租户系统。
208 127
|
2天前
|
编解码 算法 自动驾驶
【雷达通信】用于集成传感和通信的OFDM雷达传感算法(Matlab代码实现)
【雷达通信】用于集成传感和通信的OFDM雷达传感算法(Matlab代码实现)
181 125
|
2天前
|
JavaScript 关系型数据库 MySQL
基于python的网上外卖订餐系统
本系统基于Python与Flask框架,结合MySQL数据库及Vue前端技术,实现了一个功能完善的网上订餐平台。系统涵盖餐品、订单、用户及评价管理模块,并深入研究订餐系统的商业模式、用户行为与服务质量。技术上采用HTML、PyCharm开发工具,支持移动端访问,助力餐饮业数字化转型。