RPC 中 参数传递 ImputStream 流会关闭
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                RPC 中 参数传递 ImputStream 流会关闭
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                文章目錄
- 非RPC,本地方法調(diào)用時(shí):
 - RPC調(diào)用(涉及到注冊中心,Feign等方式)時(shí):
 - RPC調(diào)用中,可將InputStream轉(zhuǎn)為byte[] 進(jìn)行參數(shù)傳遞
 - InputStream 轉(zhuǎn) byte []
 
實(shí)際工作中遇到很多需要對提交的附件處理的場景。那么就涉及到控制層和業(yè)務(wù)層之間的參數(shù)傳遞。
非RPC,本地方法調(diào)用時(shí):
@PostMapping("/import")public void import(@RequestParam("file") MultipartFile file) throws IOException{return importService.import(file.getInputStream());} @Service public class ExcelImportServiceImpl implements ExcelImportService {public void import(InputStream inputStream) {//do something} }這樣是完全可以的。
RPC調(diào)用(涉及到注冊中心,Feign等方式)時(shí):
傳遞到消費(fèi)者的流都是被關(guān)閉的,導(dǎo)致空指針等異常。
java.lang.NullPointerException: nullat com.alibaba.excel.ExcelReader.finish(ExcelReader.java:277) ~[easyexcel-2.1.6.jar!/:?]at com.alibaba.excel.ExcelReader.finalize(ExcelReader.java:287) [easyexcel-2.1.6.jar!/:?]at java.lang.System$2.invokeFinalize(System.java:1270) [?:1.8.0_191]at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:102) [?:1.8.0_191]at java.lang.ref.Finalizer.access$100(Finalizer.java:34) [?:1.8.0_191]at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:217) [?:1.8.0_191]RPC調(diào)用中,可將InputStream轉(zhuǎn)為byte[] 進(jìn)行參數(shù)傳遞
@PostMapping("/import")public void import(@RequestParam("file") MultipartFile file) throws IOException{return importService.import(file.getBytes());} @Service public class ExcelImportServiceImpl implements ExcelImportService {public void import(byte[] bytes) {ByteArrayInputStream inputStream= new ByteArrayInputStream(bytes);//do something} }InputStream 轉(zhuǎn) byte []
public static byte[] toByteArray(InputStream input) throws IOException {ByteArrayOutputStream output = new ByteArrayOutputStream();byte[] buffer = new byte[1024*4];int n = 0;while (-1 != (n = input.read(buffer))) {output.write(buffer, 0, n);}return output.toByteArray(); } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的RPC 中 参数传递 ImputStream 流会关闭的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: tomcat9控制台中文乱码
 - 下一篇: 系统架构设计师 - 软件架构设计 - 基