上传文件
@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());
}
}
















