Springboot与vue实现数据导出方法具体介绍

作者:进击的Coders 时间:2023-11-06 21:00:34 

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

前言

这两天在项目中使用到Java的导入导出功能,以前对这块有一定了解,但是没有系统学习过,今天在这里进行记录,方便以后查阅。

一、需求

项目的需求是将项目中的JSON实体数据导出为.json文件,导出的文件,可以作为元数据导入进行实体初始化。项目不是使用普通的springboot框架(普通的springboot框架很容易完成),因此走了一些弯路,在这篇文章中,将先讲解使用springboot框架进行导出,然后再讲解非springboot框架的导出。

二、Springboot进行数据导出

1.Java后端代码

@RequestMapping("/download")
public void download(String path, HttpServletResponse response) {
// 固定写法
response.setContentType("application/OCTET-STREAM;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
File file = new File(path);
try {
   InputStream fis = new FileInputStream(file);
   OutputStream out = new BufferedOutputStream(response.getOutputStream());
   byte[] buffer = new byte[1024];
   int len;
   while ((len = fis.read(buffer)) != -1) {
       out.write(buffer, 0, len);
       out.flush();
   }
} catch (Exception e) {
throw new RuntimeException(e);
}
}

导出操作时,返回值类型必须为void,由于项目有指定的返回格式(返回值类型不能为void),导致类型不匹配报错,在新写了处理方法之后,解决了这个问题。 这里使用了BufferedOutputStream,能加快导出速度。不用BufferedOutputStream,只使用response.getOutputStream()也是可以的。此外,这里使用了循环写入输出流中,而不是一次写入。

2.Vue前端代码

handleExport(row) {
     const id = row.id || this.ids
     const name = row.name || this.names[0]
     const code = row.code || this.codes
     const params = {
       exportCodes: JSON.stringify(code)
     }
     this.download('/Thingmax/Things/Export',  params,  name + '.json')
   },

download方法第一个参数是导出方法的响应路由,第二个参数为导出时携带的参数,第三个参数为导出的文件名称。

3.其他几种Java后端导出方法

1、使用BufferedOutputStream,一次性写入

exportEntities.put("Entities", entities);
String content = exportEntities.toJSONString();
try {
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
out.write(content.getBytes(StandardCharsets.UTF_8));
out.flush();
out.close();
} catch (Exception e) {
    throw new RuntimeException(e);
}

一次性将数据读取到内存,通过响应输出流输出到前端

2、不使用BufferedOutputStream,循环写入

InputStream inputStream = new FileInputStream(path);
ServletOutputStream outputStream = response.getOutputStream();
  byte[] b = new byte[1024];
  int len;
  //从输入流中读取一定数量的字节,并将其存储在缓冲区字节数组中,读到末尾返回-1
  while ((len = inputStream.read(b)) > 0) {
      outputStream.write(b, 0, len);
  }

来源:https://blog.csdn.net/qq_43403676/article/details/126923857

标签:Springboot,Vue,数据导出
0
投稿

猜你喜欢

  • Activity透明/半透明效果的设置transparent(两种实现方法)

    2022-11-22 00:21:27
  • Android从0到完整项目(1)使用Android studio 创建项目详解

    2022-12-13 13:48:21
  • Spring @Conditional注解原理解析

    2022-10-04 16:09:51
  • C#对DataTable中的某列进行分组

    2021-12-10 23:06:45
  • C++ opencv实现在图片上画一条线示例代码

    2021-12-15 12:34:27
  • 详解Java ES多节点任务的高效分发与收集实现

    2021-08-03 13:59:02
  • 详解备忘录模式及其在Java设计模式编程中的实现

    2023-08-24 22:34:02
  • C#用委托BeginInvoke做异步线程

    2023-04-22 07:23:05
  • Flutter应用框架搭建实现屏幕适配方案详解

    2023-09-19 08:16:27
  • java poi解析word的方法

    2023-08-28 07:31:28
  • C# Distinct和重写IEqualityComparer时要知道的二三事

    2023-07-11 15:41:20
  • C# List<T> Contains<T>()的用法小结

    2021-05-29 11:44:56
  • Android表格自定义控件使用详解

    2023-12-23 23:35:36
  • c#和java base64不一致的解决方法

    2022-11-24 02:18:55
  • Struts2实现文件上传时显示进度条功能

    2021-10-13 05:22:22
  • 在Java中避免NullPointerException的解决方案

    2023-10-17 04:47:00
  • MyBatis超详细讲解如何实现分页功能

    2023-08-22 23:06:51
  • Mybatis的特点及优点

    2022-11-19 16:27:54
  • Kotlin协程概念原理与使用万字梳理

    2023-03-28 21:29:32
  • SpringBoot整合MyCat实现读写分离的方法

    2022-03-05 23:37:45
  • asp之家 软件编程 m.aspxhome.com