下载excel

  1. public static <T> void export(HttpServletResponse response, String fileName, String sheetName, List<T> dataList, Class<T> clazz) {
  2. try {
  3. response.setContentType("application/vnd.ms-excel");
  4. response.setCharacterEncoding(Charsets.UTF_8.name());
  5. fileName = URLEncoder.encode(fileName, Charsets.UTF_8.name());
  6. response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
  7. EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(dataList);
  8. } catch (Throwable var6) {
  9. throw var6;
  10. }
  11. }

下载zip(文件)

  1. File zip = ZipUtil.zip(folderPath);
  2. try (InputStream fileBufferInStream = new BufferedInputStream(new FileInputStream(zip));
  3. OutputStream netOutStream = new BufferedOutputStream(response.getOutputStream());) {
  4. // 将zip读入到内存的buffer
  5. byte[] buffer = new byte[fileBufferInStream.available()];
  6. int read = fileBufferInStream.read(buffer);
  7. if (read == -1) {
  8. // 读入失败
  9. throw new ServiceException("生成失败");
  10. }
  11. // 写入网络流
  12. response.setContentType("application/octet-stream");
  13. response.setCharacterEncoding(Charsets.UTF_8.name());
  14. String fileName = URLEncoder.encode("视频监控信息", Charsets.UTF_8.name());
  15. response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".zip");
  16. netOutStream.write(buffer);
  17. netOutStream.flush();
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }

Http常见的Content-type

https://www.yuque.com/docs/share/3725d74c-70b2-4aaa-83c5-ade535440da9?# 《【1】常见的content-type》