[Spring Boot] 利用MultipartFile进行文件上传和下载 的开发

本文介绍了在Spring Boot中利用MultipartFile进行文件上传和下载的操作。讲解了MultipartFile对象的方法,如getBytes()、getContentType()等,并提到了MockMultipartFile用于测试。配置方面,讨论了如何在application.properties中设定文件大小限制。最后,文章提及了使用Spring Web Mock进行文件上传的测试。
摘要由CSDN通过智能技术生成

MultipartFile

在Spring MVC中,Apache提供了一个Commons FileUpload库方便我们进行文件的上传


Package org.springframework.web.multipart
Multipart resolution framework for handling file uploads.**
Interface MultipartFile

    All Superinterfaces:
        InputStreamSource

    All Known Implementing Classes:
        CommonsMultipartFile, MockMultipartFile 

Controller

通过List<>传递多个文件,也可以通过一个MultipartFile[]数组的方式,
而如果你需要一次只传递一个文件,只需将参数声明为MultipartFile multipartFile
@PostMapping(value = "/files", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)   <=== 设置content
public ResponseEntity<Map<String, String>> uploadFile(@RequestParam(value = "file", required = true)
                                                                              List<MultipartFile> multipartFile){
        
        return null;
    }

一个MultipartFile对象中包含了以下方法:

  1. getBytes()** 返回file的二进制数组形式
    当你实现了一个保存到数据库的代码时,我们通过DB工具对文件data的column进行查看可以发现保存的内容

在这里插入图片描述

  1. getContentType() 返回String表示文件类型

Classes contained in spring-mock.jar: org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi.SimpleNamingContextBuilder.class org.springframework.mock.web.DelegatingServletInputStream.class org.springframework.mock.web.DelegatingServletOutputStream.class org.springframework.mock.web.HeaderValueHolder.class org.springframework.mock.web.MockExpressionEvaluator.class org.springframework.mock.web.MockFilterChain.class org.springframework.mock.web.MockFilterConfig.class org.springframework.mock.web.MockHttpServletRequest.class org.springframework.mock.web.MockHttpServletResponse.class org.springframework.mock.web.MockHttpSession.class org.springframework.mock.web.MockMultipartFile.class org.springframework.mock.web.MockMultipartHttpServletRequest.class org.springframework.mock.web.MockPageContext.class org.springframework.mock.web.MockRequestDispatcher.class org.springframework.mock.web.MockServletConfig.class org.springframework.mock.web.MockServletContext.class org.springframework.mock.web.PassThroughFilterChain.class org.springframework.mock.web.portlet.MockActionRequest.class org.springframework.mock.web.portlet.MockActionResponse.class org.springframework.mock.web.portlet.MockMultipartActionRequest.class org.springframework.mock.web.portlet.MockPortalContext.class org.springframework.mock.web.portlet.MockPortletConfig.class org.springframework.mock.web.portlet.MockPortletContext.class org.springframework.mock.web.portlet.MockPortletPreferences.class org.springframework.mock.web.portlet.MockPortletRequest.class org.springframework.mock.web.portlet.MockPortletRequestDispatcher.class org.springframework.mock.web.portlet.MockPortletResponse.class org.springframework.mock.web.portlet.MockPortletSession.class org.springframework.mock.web.portlet.MockPortletURL.class org.springframework.mock.web.portlet.MockRenderRequest.class org.springframework.mock.web.portlet.MockRenderResponse.class org.springframework.test.AbstractDependencyInjectionSpringContextTests.class org.springframework.test.AbstractSingleSpringContextTests.class org.springframework.test.AbstractSpringContextTests.class org.springframework.test.AbstractTransactionalDataSourceSpringContextTests.class org.springframework.test.AbstractTransactionalSpringContextTests.class org.springframework.test.AssertThrows.class org.springframework.test.ConditionalTestCase.class org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.class org.springframework.test.annotation.DirtiesContext.class org.springframework.test.annotation.ExpectedException.class org.springframework.test.annotation.IfProfileValue.class org.springframework.test.annotation.NotTransactional.class org.springframework.test.annotation.ProfileValueSource.class org.springframework.test.annotation.Repeat.class org.springframework.test.annotation.SystemProfileValueSource.class org.springframework.test.annotation.Timed.class org.springframework.test.jpa.AbstractAspectjJpaTests.class org.springframework.test.jpa.AbstractJpaTests.class org.springframework.test.jpa.OrmXmlOverridingShadowingClassLoader.class org.springframework.test.web.AbstractModelAndViewTests.class
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值