springboot 读取本地文件,提供下载功能

x33g5p2x  于2021-12-16 转载在 Spring  
字(0.6k)|赞(0)|评价(0)|浏览(462)
import org.apache.commons.io.IOUtils

@GetMapping("/downloadPDF/{billingCycle}")
	fun downloadPDFBill(
		@PathVariable billingCycle: YearMonth,
		response: HttpServletResponse
	) {
		response.contentType = "application/x-download"
		response.characterEncoding="UTF-8"
		response.setHeader("Content-Disposition", "attachment;filename=${billingCycle}-bill.pdf")
		val realPath="D:\\data\\2011-12-bill.pdf"
		val inputStream = FileInputStream(realPath)
		val outputStream = response.outputStream
		IOUtils.copy(inputStream, outputStream)
		outputStream.flush()
	}

相关文章