webflux上传下载文件
生活随笔
收集整理的這篇文章主要介紹了
webflux上传下载文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上傳文件
@PostMapping("addAttach") public Mono<JsonResult> addAttach(@RequestPart("file") FilePart filePart,//獲取文件參數@RequestPart("dataId") String dataId,//獲取其他參數 ){String strFileName = filePart.filename();//獲取文件名File file = new File(strNewFilePath);filePart.transferTo(file);//轉儲文件JsonResult result=……return Mono.just(result); }注意:獲取文件用RequestPart,接收參數類型為FilePart,同方式的其他參數也需要用RequestPart獲取。
下載文件
@GetMapping("downloadFile")public Mono<Void> downloadFile(Long fileId, ServerHttpResponse response){FFile fFile= fileService.getFile(fileId);if(fFile==null) {return ServerHttpResponseUtil.writeHtml(response,"<html><head><meta charset=\"utf-8\"/></head><body>文件不存在!</body></html>");}else {String strFilePath = fileConfig.getStoreBasePath() + fFile.getStorePath();File file = new File(strFilePath);ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response;try {response.getHeaders().set(HttpHeaders.CONTENT_DISPOSITION,"attachment; filename=" + new String(fFile.getFileName().getBytes("UTF-8"), "iso-8859-1"));//輸出文件名亂碼問題處理} catch (UnsupportedEncodingException e) {e.printStackTrace();}response.getHeaders().setContentType(MediaType.APPLICATION_OCTET_STREAM);return zeroCopyResponse.writeWith(file, 0, file.length());}}ServerHttpResponseUtil.writeHtml方法參見https://blog.csdn.net/whq12789/article/details/90085649
總結
以上是生活随笔為你收集整理的webflux上传下载文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Manjaro使用笔记-使用中国源的方法
- 下一篇: 四,Golang 交叉编译