1. webFlux 的下载
首先 webflux中没有HttpServletResponse,但是在webflux中提供了org.springframework.http.server.reactive.ServerHttpResponse,但是在注入的时候要注意自己的项目中的jar包,不然在将ServerHttpResponse转换为同样继承了 ReactiveHttpOutputMessage的类ZeroCopyHttpOutputMessage,spring在注入ServerHttpResponse可能会将其注入为其他的,像我就遇到了这个问题
这是我遇到的问题信息
java.lang.ClassCastException: org.springframework.http.server.reactive.TomcatHttpHandlerAdapter$TomcatServerHttpResponse cannot be cast to org.springframework.http.ZeroCopyHttpOutputMessage
at com.rbs.apisix.controller.EnvFileController.exportExcel(EnvFileController.java:120)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
<!--统一处理工具类-->
<dependency>
<groupId>com.rbs</groupId>
<artifactId>common-tool</artifactId>
<version>1.0.7</version>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
</exclusions>
</dependency>
然后是文件下载的代码,我这里写的可能多了点
public Mono<Void> export(ServerHttpResponse response) {
ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response;
String fileName = System.currentTimeMillis() + ".txt";
File finalFile = null;
try {
String content ="测试文件导出";
HttpHeaders headers = zeroCopyResponse.getHeaders();
headers.set("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
headers.set("file-name", URLEncoder.encode(fileName, "UTF-8"));
headers.set("Access-Control-Allow-Origin", "*");
MediaType application = new MediaType("application", "octet-stream", Charset.forName("UTF-8"));
headers.setContentType(application);
FileOutputStream fout = new FileOutputStream(fileName);
byte[] bytes = content.getBytes("UTF-8");
fout.write(bytes);
fout.close();
finalFile = new File(fileName);
zeroCopyResponse.writeWith(finalFile, 0, finalFile.length()).subscribe();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
} finally {
if (finalFile != null) {
finalFile.delete();
}
}
return null;
}
然后还有一种写法是直接使用的ServerHttpResponse,这种我只是测试了下
public Mono<ServerResponse> test() throws IOException {
String content = JSON.toJSONString("test");
FileOutputStream fout = new FileOutputStream("test.txt");
byte[] bytes = content.getBytes("UTF-8");
fout.write(bytes);
fout.close();
File finalFile = new File("test.txt");
return ServerResponse.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=test.txt")
.contentType(new MediaType("multipart/form-data"))
.body((p, a) -> {
ZeroCopyHttpOutputMessage resp = (ZeroCopyHttpOutputMessage) p;
return resp.writeWith(finalFile, 0, finalFile.length());
}).doFinally(a -> {finalFile.delete();});
}
2. webFlux 的文件上传
我就只贴出部分代码了,这里使用@RequestPart注解接收文件,然后FilePart .transferTo()能够将文件转换为file至于之后的操作就要看你的业务要求了
public Mono<CommonResult<String>> exportImport(@RequestPart("file") FilePart file) throws IOException, InvalidFormatException, IllegalAccessException, InstantiationException {
File file1 = new File(file.filename());
file.transferTo(file1);
}