@RestController
@RequestMapping(value = "/file")
public class FileController {
/**
* 大文件读取(使用 StreamingResponseBody 实现流式下载)
*/
@GetMapping("/large-data")
public StreamingResponseBody downloadLargeData(HttpServletResponse response) {
// 设置响应头
response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment; filename=\"large-data.csv\"");
return new StreamingResponseBody() {
@Override
public void writeTo(OutputStream outputStream) throws IOException {
// 模拟分页查询数据
int pageSize = 10000; // 每页大小
int page = 0;
while (true) {
// 从数据库或其他数据源中分页读取数据
List<String> data = fetchDataFromDatabase(page, pageSize);
if (data.isEmpty()) {
break; // 数据读取完毕
}
// 将数据写入响应流
for (String line : data) {
outputStream.write((line + "\n").getBytes());
}
outputStream.flush();
page++;
}
}
};
}
// 模拟从数据库分页读取数据
private List<String> fetchDataFromDatabase(int page, int pageSize) {
// 这里可以替换为实际的数据库查询逻辑
// 例如:SELECT * FROM large_table LIMIT pageSize OFFSET page * pageSize
if (page >= 10) { // 假设总共有10页数据
return List.of();
}
return List.of("Data line " + (page * pageSize + 1), "Data line " + (page * pageSize + 2));
}
/**
* 使用文件分块下载(文件已经存在)
*
* @param response
* @return
* @throws IOException
*/
@GetMapping("/large-file")
public StreamingResponseBody downloadLargeFile(HttpServletResponse response) throws IOException {
File file = new File("/path/to/large-file.zip");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
InputStream inputStream = new FileInputStream(file);
return outputStream -> {
int nRead;
byte[] data = new byte[1024];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
outputStream.write(data, 0, nRead);
}
inputStream.close();
};
}
@GetMapping("/export1")
public void export() {
// 数据赋值
List<ProductLabel> productLabelList = new ArrayList<>();
ProductLabel productLabel1 = new ProductLabel();
productLabel1.setLabelId("国补");
productLabel1.setSequence(1);
productLabelList.add(productLabel1);
ProductLabel productLabel2 = new ProductLabel();
productLabel2.setLabelId("高性价比");
productLabel2.setSequence(2);
productLabelList.add(productLabel2);
ProductLabel productLabel3 = new ProductLabel();
productLabel3.setLabelId("库存不多");
productLabel3.setSequence(5);
productLabelList.add(productLabel3);
ProductInfo productInfo = new ProductInfo();
productInfo.setProductId("1001");
productInfo.setProductName("华为Mate 40");
productInfo.setPrice(2999.00f);
productInfo.setCreateTime(new Date());
productInfo.setProductLabelList(productLabelList);
// 创建ObjectMapper对象
ObjectMapper objectMapper = new ObjectMapper();
try {
// 将实体对象转换为JSON字符串
String jsonString = objectMapper.writeValueAsString(productInfo);
System.out.println("JSON String: " + jsonString);
// 将JSON字符串写入文件
File file = new File("product.json"); // 不加路径,直接存储到项目更目录
objectMapper.writeValue(file, productInfo); // 此处不能写入JSON字符串,否则文件会存储含转义符的字符数据
System.out.println("JSON data written to file: " + file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 实体对象转换JSON格式导出
*
* @param request
* @param response
* @throws IOException
*/
@GetMapping("/export")
public void export(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 数据赋值
List<ProductLabel> productLabelList = new ArrayList<>();
ProductLabel productLabel1 = new ProductLabel();
productLabel1.setLabelId("国补");
productLabel1.setSequence(1);
productLabelList.add(productLabel1);
ProductLabel productLabel2 = new ProductLabel();
productLabel2.setLabelId("高性价比");
productLabel2.setSequence(2);
productLabelList.add(productLabel2);
ProductLabel productLabel3 = new ProductLabel();
productLabel3.setLabelId("库存不多");
productLabel3.setSequence(5);
productLabelList.add(productLabel3);
ProductInfo productInfo = new ProductInfo();
productInfo.setProductId("1001");
productInfo.setProductName("华为Mate 40");
productInfo.setPrice(2999.00f);
productInfo.setCreateTime(new Date());
productInfo.setProductLabelList(productLabelList);
// 创建ObjectMapper对象
ObjectMapper objectMapper = new ObjectMapper();
// 下载文件
// 设置响应头
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"product.json\"");
// 将实体对象写入响应流
objectMapper.writeValue(response.getOutputStream(), productInfo);
}
/**
* 导入JSON文件再转换实体对象
*
* @param
* @return
* @date 2025/2/20 20:50
*/
@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) throws IOException {
if (file.isEmpty()) {
return "文件不能为空";
}
String jsonContent = new String(file.getBytes(), StandardCharsets.UTF_8);
System.out.println("读取到的文件内容:" + jsonContent);
ObjectMapper objectMapper = new ObjectMapper();
ProductInfo productInfo = objectMapper.readValue(jsonContent, ProductInfo.class);
String productId = productInfo.getProductId();
List<ProductLabel> productLabelList = productInfo.getProductLabelList();
System.out.println("取到了数据:" + productId);
return "取到了数据";
}
调用下载方法示例: