SpringBoot文件上傳同時接收復雜參數(shù)的過程詳解
環(huán)境信息
Spring Boot:2.0.8.RELEASE
Spring Boot內(nèi)置的tomcat:tomcat-embed-core 8.5.37
問題描述
收到文件上傳的開發(fā)工作,要求能適配各種場景,并且各場景的請求參數(shù)不一樣,因此接收的參數(shù)不能是固定的幾個字段,要有類似Map的字段來接收動態(tài)參數(shù)。
擬使用MultipartFile[] files來接收文件列表,用自定義對象UploadFileDto來接收上傳參數(shù)(里面包含一個Map)
UploadFileDto.java
@ApiModel("文件上傳dto") @Data public class UploadFileDto { @ApiModelProperty("處理器,在Spring容器里的名稱") private String handlerName; @ApiModelProperty("交易代碼") private String bizCode; @ApiModelProperty("交易ID") private String bizId; @ApiModelProperty("上傳配置名稱,對應配置文件里的配置") private String uploadConfigName; @ApiModelProperty("動態(tài)信息") private Map<String, Object> dynamicDto; @ApiModelProperty("上傳時間") private String uploadDt; @ApiModelProperty("限定文件大小") private long defaultFileSize; @ApiModelProperty("限定文件格式,多個用逗號隔開") private String defaultFileSuffix; @ApiModelProperty("默認路徑之后的文件上級目錄") private String parentDir; @ApiModelProperty("備注信息") private String remark; @ApiModelProperty("上傳后的文件信息") private List<FileInfoDto> fileInfoDtos; }
Controller:
@RestController @RequestMapping("/fileCommmon") @Api(tags = "公共-文件操作") public class FileUpDownLoadController extends BaseController { @Resource private UpDownLoadService upDownLoadService; @PostMapping(value = "/upload") @ApiOperation("文件上傳") public ResponseDto<UploadFileDto> uploadFile(@RequestParam("files") MultipartFile[] files, @RequestPart() UploadFileDto uploadFileDto) { upDownLoadService.uploadFile(uploadFileDto, files); return getResponseDto(uploadFileDto); } }
Controller方法,使用@RequestParam("files") MultipartFile[] files來接收文件列表
使用@RequestPart() UploadFileDto uploadFileDto來接收復雜參數(shù)。
UpDownLoadService是上傳實現(xiàn)類,這里暫時不暫時源碼。
可是,在訪問該接口的時候,報錯了:org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:226)
at org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver.resolveArgument(RequestPartMethodArgumentResolver.java:134)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:124)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:131)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:891)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:981)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:884)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:858)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
錯誤分析
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported,意思是應用不支持'application/octet-stream'這種文件類型
Swagger2里的請求參數(shù):
分析異常堆棧,是在AbstractMessageConverterMethodArgumentResolver的readWithMessageConverters方法里拋出的:
分析這個方法里的代碼,發(fā)現(xiàn)Spring在嘗試使用各個消息轉(zhuǎn)換器(HttpMessageConverter)來處理請求。這里的contentType值是'application/octet-stream',發(fā)現(xiàn)找不到可用的消息轉(zhuǎn)換器:
genericConverter.canRead(targetType, contextClass, contentType) ,因此無法解析、轉(zhuǎn)換消息,因此拋出了異常。
確切的說,上面說的是處理請求參數(shù)uploadFileDto,第一個請求參數(shù)files已經(jīng)在下面這個地方處理了(文件類型參數(shù)使用MultipartResolutionDelegate.resolveMultipartArgument來處理):
注:
messageConverters的值(這個看各個應用里注冊的消息轉(zhuǎn)換器類型、順序):
經(jīng)常使用的是MappingJackson2HttpMessageConverter消息轉(zhuǎn)換器,這個是SpringBoot默認用來處理消息的,將原始請求轉(zhuǎn)成json格式,再轉(zhuǎn)成目標類型的格式,是Jackson
targetClass是UploadFileDto對應的Class
解決方法
明白了問題產(chǎn)生的原因之后,就有了解決思路:增加一個消息轉(zhuǎn)換器,用于支持'application/octet-stream'。
最簡單的添加方式,新增一個類,繼承AbstractJackson2HttpMessageConverter,這個也是上面說的MappingJackson2HttpMessageConverter的父類
@Component public class MultipartJackson2HttpMessageConverter extends AbstractJackson2HttpMessageConverter { /** * Converter for support http request with header Content-Type: multipart/form-data */ public MultipartJackson2HttpMessageConverter(ObjectMapper objectMapper) { super(objectMapper, MediaType.APPLICATION_OCTET_STREAM); } @Override public boolean canWrite(Type type, Class<?> clazz, MediaType mediaType) { return false; } @Override public boolean canWrite(Class<?> clazz, MediaType mediaType) { return false; } @Override protected boolean canWrite(MediaType mediaType) { return false; } }
加了上面的轉(zhuǎn)換器之后:
讀取之后的body類型就是UploadFileDto
至此,問題解決,后端程序能夠正常接收、處理前端發(fā)過來的文件列表,以及復雜參數(shù)。
簡單參數(shù)
如果后端程序不需要接收復雜參數(shù),而是只需要固定的幾個簡單參數(shù),那么就很簡單,比如:
Controller:
@PostMapping(value = "/upload2") @ApiOperation("文件上傳2") public ResponseDto<String> uploadFile(@RequestParam("files") MultipartFile[] files, @RequestPart() String remark) { System.out.println("remark = " + remark); upDownLoadService.uploadFile(remark, files); return getResponseDto(remark); }
此時,contentType值是'application/octet-stream'的String的參數(shù),Spring有提供了默認的消息轉(zhuǎn)換器StringHttpMessageConverter,支持所有的格式,就不需要自定義MappingJackson2HttpMessageConverter消息轉(zhuǎn)換器了。
總結(jié)
回顧一下,如果需要復雜參數(shù)(自定義對象接收前端參數(shù)),那么需要自定義消息轉(zhuǎn)換器(如AbstractJackson2HttpMessageConverter),來支持contentType值是'application/octet-stream'類型的參數(shù),并將其轉(zhuǎn)換成目標格式;
如果不需要復雜參數(shù),只是String等類型,那么不需要自定義消息轉(zhuǎn)換器;
消息轉(zhuǎn)換器是SpringBoot處理前端傳輸?shù)臄?shù)據(jù),并轉(zhuǎn)換成接口參數(shù)的類型的轉(zhuǎn)換器,轉(zhuǎn)換前、轉(zhuǎn)換后還支持自定義插件處理。詳見AbstractMessageConverterMethodArgumentResolver的readWithMessageConverters
@RequestParam和@RequestPart的區(qū)別,可以自行查找資料
參考文獻:
rest - @RequestPart with mixed multipart request, Spring MVC 3.2 - Stack Overflow
@RequestPart同時接收文件和json后端報錯 - 簡書 (jianshu.io)
到此這篇關(guān)于SpringBoot文件上傳同時,接收復雜參數(shù)的文章就介紹到這了,更多相關(guān)SpringBoot文件上傳接收參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot使用@PostConstruct注解導入配置方式
這篇文章主要介紹了SpringBoot使用@PostConstruct注解導入配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Spring boot 實現(xiàn)單個或批量文件上傳功能
這篇文章主要介紹了Spring boot 實現(xiàn)單個或批量文件上傳功能,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2018-08-08使用Java?Socket實現(xiàn)GPS定位數(shù)據(jù)處理
在許多應用場景中,如車輛追蹤、移動設備定位等,GPS定位數(shù)據(jù)的實時獲取和處理至關(guān)重要,本文將介紹如何使用Java?Socket編程來接收GPS設備發(fā)送的數(shù)據(jù)并進行處理,需要的朋友可以參考下2024-07-07Java中ArrayList和LinkedList的遍歷與性能分析
這篇文章主要給大家介紹了ArrayList和LinkedList這兩種list的五種循環(huán)遍歷方式,各種方式的性能測試對比,根據(jù)ArrayList和LinkedList的源碼實現(xiàn)分析性能結(jié)果,總結(jié)結(jié)論。相信對大家的理解和學習具有一定的參考價值,有需要的朋友們下面跟著小編一起來學習學習吧。2016-12-12