当前位置:   article > 正文

解决Springboot打包后下载文件内容为空_springboot 下载excel模板 文件内容丢失

springboot 下载excel模板 文件内容丢失
	/**
	 * 根据文件名下载模板
	 *
	 * @param response
	 * @param fileName
	 */
	@Override
	public void downloadTemplate(HttpServletResponse response, String fileName) {
		OutputStream os = null;
		InputStream is = null;
		try {
			InputStream inputStream = this.getClass().getResourceAsStream("/template/" + fileName);
			File somethingFile = File.createTempFile("template", ".xlsx");
			try {
				FileUtils.copyInputStreamToFile(inputStream, somethingFile);
			} finally {
				inputStream.close();
			}
			InputStream in = new FileInputStream(somethingFile);
			// 取得输出流
			os = response.getOutputStream();
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-Disposition", "attachment;filename=1.xlsx");
			//复制
			IOUtils.copy(in, response.getOutputStream());
			response.getOutputStream().flush();
		} catch (IOException e) {
			System.out.println("下载文件失败," + e.getMessage());
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
			}
			try {
				if (os != null) {
					os.close();
				}
			} catch (IOException e) {
			}
		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/815973
推荐阅读
相关标签
  

闽ICP备14008679号