- 一、簡(jiǎn)介
-
二、工程搭建
- 1、工程結(jié)構(gòu)
- 2、依賴管理
-
三、上傳下載
- 1、配置管理
- 2、上傳下載
-
四、Excel文件
- 1、Excel創(chuàng)建
- 2、Excel讀取
- 3、解析監(jiān)聽
- 4、導(dǎo)入導(dǎo)出
- 五、參考源碼
標(biāo)簽:上傳.下載.Excel.導(dǎo)入.導(dǎo)出;
一、簡(jiǎn)介
在項(xiàng)目中,文件管理是常見的復(fù)雜功能;
首先文件的類型比較多樣,處理起來比較復(fù)雜,其次文件涉及大量的IO操作,容易引發(fā)內(nèi)存溢出;
不同的文件類型有不同的應(yīng)用場(chǎng)景;
比如:圖片常用于頭像和證明材料;Excel偏向業(yè)務(wù)數(shù)據(jù)導(dǎo)入導(dǎo)出;CSV偏向技術(shù)層面數(shù)據(jù)搬運(yùn);PDF和Word用于文檔類的材料保存等;
下面的案例只圍繞普通文件和Excel兩種類型進(jìn)行代碼實(shí)現(xiàn);
二、工程搭建
1、工程結(jié)構(gòu)
2、依賴管理
普通文件的上傳下載,依賴spring-boot
框架即可,而Excel類型選擇easyexcel
組件,該組件內(nèi)部依賴了apache-poi
組件的4.1.2
版本;
<!-- 基礎(chǔ)框架組件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Excel組件 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>${easyexcel.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
三、上傳下載
1、配置管理
在配置文件中,添加max-file-size
單個(gè)文件大小限制和max-request-size
請(qǐng)求最大限制兩個(gè)核心參數(shù);
需要說明的一點(diǎn)是:如何設(shè)定參數(shù)值的大小,與業(yè)務(wù)場(chǎng)景和服務(wù)器的處理能力都有關(guān)系,在測(cè)試的過程中優(yōu)化即可;
spring:
# 文件配置
servlet:
multipart:
enabled: true
# 文件單個(gè)限制
max-file-size: 10MB
# 請(qǐng)求最大限制
max-request-size: 20MB
2、上傳下載
這里提供一個(gè)文件批量上傳接口和一個(gè)文件下載接口,把文件管理在工程中的resources/file
目錄下,下載接口中需要指定該目錄下的文件名稱;
@RestController
public class FileWeb {
private static final Logger logger = LoggerFactory.getLogger(FileWeb.class);
@Resource
private FileService fileService ;
/**
* 文件上傳
*/
@PostMapping("/file/upload")
public String upload (HttpServletRequest request,
@RequestParam("file") MultipartFile[] fileList) throws Exception {
String uploadUser = request.getParameter("uploadUser");
if (uploadUser.isEmpty()){
return "upload-user is empty";
}
logger.info("upload-user:{}",uploadUser);
for (MultipartFile multipartFile : fileList) {
// 解析文件信息和保存
fileService.dealFile(multipartFile);
}
return "success" ;
}
/**
* 文件下載
*/
@GetMapping("/file/download")
public void upload (@RequestParam("fileName") String fileName,
HttpServletResponse response) throws Exception {
if (!fileName.isBlank()){
String filePath = ResourceUtils.getURL("m1-04-boot-file/src/main/resources/file").getPath();
File file = new File(filePath,fileName) ;
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
response.setContentType("application/octet-stream");
Files.copy(Paths.get(file.getPath()), response.getOutputStream());
}
}
}
/**
* 文件服務(wù)類
*/
@Service
public class FileService {
private static final Logger logger = LoggerFactory.getLogger(FileService.class);
public void dealFile (MultipartFile multipartFile) throws Exception {
logger.info("Name >> {}",multipartFile.getName());
logger.info("OriginalFilename >> {}",multipartFile.getOriginalFilename());
logger.info("ContentType >> {}",multipartFile.getContentType());
logger.info("Size >> {}",multipartFile.getSize());
// 文件輸出地址
String filePath = ResourceUtils.getURL("m1-04-boot-file/src/main/resources/file").getPath();
File writeFile = new File(filePath, multipartFile.getOriginalFilename());
multipartFile.transferTo(writeFile);
}
}
使用Postman測(cè)試文件批量上傳接口:
四、Excel文件
1、Excel創(chuàng)建
基于easyexcel
組件中封裝的EasyExcel
工具類,繼承自EasyExcelFactory
工廠類,實(shí)現(xiàn)Excel單個(gè)或多個(gè)Sheet
的創(chuàng)建,并且在單個(gè)Sheet
中寫多個(gè)Table
數(shù)據(jù)表;
@Service
public class ExcelService {
/**
* Excel-寫單個(gè)Sheet
*/
public static void writeSheet () throws Exception {
// 文件處理
String basePath = getAbsolutePath();
File file = new File(basePath+"/easy-excel-01.xlsx") ;
checkOrCreateFile(file);
// 執(zhí)行寫操作
EasyExcel.write(file).head(DataVO.class)
.sheet(0,"用戶信息").doWrite(DataVO.getSheet1List());
}
/**
* Excel-寫多個(gè)Sheet
*/
public static void writeSheets () throws Exception {
// 文件處理
String basePath = getAbsolutePath();
File file = new File(basePath+"/easy-excel-02.xlsx") ;
checkOrCreateFile(file);
ExcelWriter excelWriter = null;
try {
excelWriter = EasyExcel.write(file).build();
// Excel-Sheet1
WriteSheet writeSheet1 = EasyExcel.writerSheet(0,"分頁(yè)1").head(DataVO.class).build();
// Excel-Sheet2
WriteSheet writeSheet2 = EasyExcel.writerSheet(1,"分頁(yè)2").head(DataVO.class).build();
// Excel-Sheet3,寫兩個(gè)Table
WriteSheet writeSheet3 = EasyExcel.writerSheet(2,"分頁(yè)3").build();
WriteTable dataTable = EasyExcel.writerTable(0).head(DataVO.class).build();
WriteTable dataExtTable = EasyExcel.writerTable(1).head(DataExtVO.class).build();
// 執(zhí)行寫操作
excelWriter.write(DataVO.getSheet1List(), writeSheet1);
excelWriter.write(DataVO.getSheet2List(), writeSheet2);
excelWriter.write(DataVO.getSheet1List(),writeSheet3,dataTable) ;
excelWriter.write(DataExtVO.getSheetList(),writeSheet3,dataExtTable) ;
} catch (Exception e){
e.printStackTrace();
} finally {
if (excelWriter != null){
excelWriter.close();
}
}
}
}
/**
* 實(shí)體類,這里的注解會(huì)解析為Excel中的表頭
*/
public class DataVO {
@ExcelProperty("編號(hào)")
private Integer id ;
@ExcelProperty("名稱")
private String name ;
@ExcelProperty("手機(jī)號(hào)")
private String phone ;
@ExcelProperty("城市")
private String cityName ;
@ExcelProperty("日期")
private Date date ;
}
文件效果:
2、Excel讀取
對(duì)于讀取Excel文件來說,則需要根據(jù)具體的樣式來定了,在easyexcel
組件中還可以添加讀取過程的監(jiān)聽器;
@Service
public class ExcelService {
/**
* Excel-讀取數(shù)據(jù)
*/
public static void readExcel () throws Exception {
// 文件處理
String basePath = getAbsolutePath();
File file = new File(basePath+"/easy-excel-01.xlsx") ;
if (!file.exists()){
return ;
}
// 讀取數(shù)據(jù)
List<DataVO> dataList = EasyExcel.read(file).head(DataVO.class)
.sheet(0).headRowNumber(1).doReadSync();
dataList.forEach(System.out::println);
}
/**
* Excel-讀取數(shù)據(jù)使用解析監(jiān)聽器
*/
public static void readExcelListener () throws Exception {
// 文件處理
String basePath = getAbsolutePath();
File file = new File(basePath+"/easy-excel-01.xlsx") ;
if (!file.exists()){
return ;
}
// 讀取數(shù)據(jù),并且使用解析監(jiān)聽器
DataListener dataListener = new DataListener() ;
List<DataVO> dataSheetList = EasyExcel.read(file,dataListener).head(DataVO.class)
.sheet(0).headRowNumber(1).doReadSync();
dataSheetList.forEach(System.out::println);
}
}
3、解析監(jiān)聽
繼承AnalysisEventListener
類,并重寫其中的方法,可以監(jiān)聽Excel的解析過程,或者添加一些自定義的處理邏輯;
public class DataListener extends AnalysisEventListener<DataVO> {
/**
* 接收解析的數(shù)據(jù)塊
*/
@Override
public void invoke(DataVO data, AnalysisContext context) {
System.out.println("DataListener:"+data);
}
/**
* 接收解析的表頭
*/
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
System.out.println("DataListener:"+headMap);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
System.out.println("DataListener:after...all...analysed");
}
}
4、導(dǎo)入導(dǎo)出
實(shí)際上Excel文件的導(dǎo)入導(dǎo)出,原理與文件的上傳下載類似,只不過這里使用easyexcel
組件中的API來直接處理Excel的寫和讀;
@RestController
public class ExcelWeb {
@GetMapping("excel/download")
public void download(HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("Excel數(shù)據(jù)", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), DataVO.class).sheet("用戶").doWrite(DataVO.getSheet1List());
}
@ResponseBody
@PostMapping("excel/upload")
public String upload(@RequestParam("file") MultipartFile file) throws IOException {
List<DataVO> dataList = EasyExcel
.read(file.getInputStream(), DataVO.class, new DataListener()).sheet().doReadSync();
dataList.forEach(System.out::println);
return "success";
}
}
使用Postman測(cè)試單個(gè)Excel上傳接口:文章來源:http://www.zghlxwxcb.cn/news/detail-636909.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-636909.html
五、參考源碼
文檔倉(cāng)庫(kù):
https://gitee.com/cicadasmile/butte-java-note
源碼倉(cāng)庫(kù):
https://gitee.com/cicadasmile/butte-spring-parent
到了這里,關(guān)于SpringBoot3文件管理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!