CommonsMultipartFile---用Spring实现文件上传

2024-05-24 09:38

本文主要是介绍CommonsMultipartFile---用Spring实现文件上传,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


CommonsMultipartFile

Spring提供的读取文件的类,使用方便,依赖spring-web-3.1.2.RELEASE.jar

包路径:

java.lang.Object

  extended byorg.springframework.web.multipart.commons.CommonsMultipartFile

方法汇总:
byte[]getBytes() Return the contents of the file as an array of bytes.
StringgetContentType() Return the content type of the file.
FileItemgetFileItem()Return the underlying org.apache.commons.fileupload.FileItem instance
InputStreamgetInputStream()Return an InputStream to read the contents of the file from.
StringgetName()Return the name of the parameter in the multipart form.
StringgetOriginalFilename()  Return the original filename in the client's filesystem.
longgetSize()Return the size of the file in bytes.
StringgetStorageDescription() Return a description for the storage location of the multipart content.
protected  booleanisAvailable()Determine whether the multipart content is still available.
booleanisEmpty()  Return whether the uploaded file is empty, that is, either no file has been chosen in the multipart form or the chosen file has no content.
voictransferTo(File dest)   Transfer the received file to the given destination file.

 使用方法:

1.spring配置文件配置文件上传解析器 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">         <property name="defaultEncoding" value="utf-8"></property>         <property name="maxUploadSize" value="90000000" />         <property name="uploadTempDir" value="uploadFiles"></property>     </bean> 2.html写法注意两点 a.input类型为file:<input  type="file" name="sealPfxFile"  id="sealPfxFile" size="24" /> b.form中增加参数 enctype="multipart/form-data":        <form id="addSeal" name="addSeal" action="${root}/seal/o_add.do" enctype="multipart/form-data" method="post"> 3.Service的写法 (注意与html中定义的名称相同即可通过get方法取得需要的内容) public String doAction( @RequestParam("sealPfxFile") CommonsMultipartFile sealPfxFile, Seal seal, ModelMap modelMap, HttpServletRequest request) throws Exception { //上传文件名 String fileName = sealPfxFile.getFileItem().getName(); //上传文件流 InputStream is = sealPfxFile.getInputStream(); }

这篇关于CommonsMultipartFile---用Spring实现文件上传的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/998043

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Java并发编程之如何优雅关闭钩子Shutdown Hook

《Java并发编程之如何优雅关闭钩子ShutdownHook》这篇文章主要为大家详细介绍了Java如何实现优雅关闭钩子ShutdownHook,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 目录关闭钩子简介关闭钩子应用场景数据库连接实战演示使用关闭钩子的注意事项开源框架中的关闭钩子机制1.

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows