1 Maven依赖
<!--MinIO文件服务器-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.0.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
2 调试代码
/**
* 导出(上传至MinIO)
*
* @param response
*/
@GetMapping("/exportToMinIO")
public String exportToMinIO(HttpServletResponse response) {
try {
//生成表格数据
List<List<Object>> dataList = new ArrayList<>();
dataList.add(new ArrayList<>(Arrays.asList(new Object[]{"表头11", "表头2", "表头3", "表头4"})));
dataList.add(new ArrayList<>(Arrays.asList(new Object[]{"表头17777777777", "表头2", "表头3", "表头4444"})));
dataList.add(new ArrayList<>(Arrays.asList(new Object[]{"表头31", "表头2", "表头3", "表头4"})));
//导出文件
String fileName = new String("文件名称.xlsx".getBytes(), "UTF-8");
response.addHeader("Content-Disposition", "filename=" + fileName);
//设置类型
response.setContentType("application/octet-stream");
//XWPFDocument转FileItem
FileItemFactory factory = new DiskFileItemFactory(16, null);
FileItem fileItem = factory.createItem("textField", "text/plain", true, fileName);
try {
OutputStream os = fileItem.getOutputStream();;
ExcelWriter excelWriter = EasyExcel.write(os).build();
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
excelWriter.write(dataList, writeSheet);
//千万别忘记finish 会帮忙关闭流
excelWriter.finish();
os.close();
//FileItem转MultipartFile
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
return minIOService.uploadFile(minIOService.getMinioClient(),multipartFile);
} catch (Exception e) {
e.printStackTrace();
}
return null;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
3 调试结果
3.1 PostMan调试结果
3.2 MinIO调试结果
3.3 文档内容
注:
MinIOService的源码请查看以下博客。