springboot版本: 2.2.9
springcloud版本: Hoxton.SR3
处理上传(服务调用端):
编码器配置
/**
* @author Lee
* @description feign 文件转码器
* @date 2020/11/26 2:10 下午
**/
public class FeignMultipartSupportConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
@Bean
public feign.Logger.Level multipartLoggerLevel() {
return feign.Logger.Level.FULL;
}
}
feign客户端配置:
@PostMapping(value = "schoolSchedule/importExcel", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
void importExcelSchoolSchedule(@RequestPart(value = "file") MultipartFile file) throws IOException;
处理下载
服务调用端控制器
@ApiOperation(value = "学校课表管理: 导出学校课表信息模板")
@GetMapping(value = "exportTemp")
public ResponseEntity<byte[]> exportTemp() {
Response response = eduExcelServiceFeignClient.exportTempSchoolSchedule();
return FileUtil.handlerFileDown(response, "学校课表信息模板.xlsx");
}
服务调用端 feign
@GetMapping(value = "schoolSchedule/exportTemp")
Response exportTempSchoolSchedule();
服务调用端response处理工具
/**
* @author Lee
* @description 处理fein 文件下载
* @date 2020/11/26 4:08 下午
**/
public static ResponseEntity<byte[]> handlerFileDown(Response response, String fileName) {
ResponseEntity<byte[]> result = null;
InputStream inputStream = null;
try {
// feign文件下载
Response.Body body = response.body();
inputStream = body.asInputStream();
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
HttpHeaders heads = new HttpHeaders();
heads.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName);
heads.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
result = new ResponseEntity<byte[]>(b, heads, HttpStatus.OK);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
服务提供端处理
public void downloadTemp(HttpServletResponse response) throws IOException {
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map = new LinkedHashMap<>();
map.put("所属学院", "");
map.put("所属专业", "");
map.put("所属班级", "");
map.put("课程名称", "");
map.put("教师名称", "");
map.put("教师工号", "");
map.put("授课日期", "");
map.put("授课地址", "");
map.put("节次", "");
list.add(map);
FileUtil.downloadExcel(list, response);
}
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) throws IOException {
String tempPath = System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx";
File file = new File(tempPath);
BigExcelWriter writer = ExcelUtil.getBigWriter(file);
// 一次性写出内容,使用默认样式,强制输出标题
writer.write(list, true);
//response为HttpServletResponse对象
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
response.setHeader("Content-Disposition", "attachment;filename=file.xlsx");
ServletOutputStream out = response.getOutputStream();
// 终止后删除临时文件
file.deleteOnExit();
writer.flush(out, true);
//此处记得关闭输出Servlet流
IoUtil.close(out);
}