我想從網(wǎng)站的服務器下載一個 pdf 文件。當我按下一個按鈕時,我向 spring boot 服務器發(fā)送了一個 ajax 請求。我創(chuàng)建了 httpservletresponse 的輸出流并將文件字節(jié)寫入其中。在 javascript 中我得到了 pdf 文件信息,但我不知道如何下載它們。我認為問題是jquery中的調(diào)用。我不確定我是否使用了正確的內(nèi)容類型。這是我的請求的請求和響應標頭:Request-Headers Accept: application/json, text/javascript, */*; q=0.01 Accept-Encoding: gzip, deflate, br Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7 Connection: keep-alive Content-Type: application/pdf Host: localhost:8080 Referer: http://localhost:8080/arbeitsvorratsliste Sec-Fetch-Mode: cors Sec-Fetch-Site: same-origin User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3880.4 Safari/537.36 X-Requested-With: XMLHttpRequestResponse-HeadersCache-Control: no-cacheContent-Disposition: attachment; filename=44005001.25759.pdfContent-Encoding: UTF-8Content-Type: application/pdf;charset=ISO-8859-1Date: Mon, 19 Aug 2019 07:38:36 GMTExpires: 0Transfer-Encoding: chunked@GetMapping("/print.do")public void doPrint(){ final HttpServletResponse response = getResponse(); RequestUtils.setResponseHeaders(response, "44005001.25759.pdf"); Path path = Paths.get(settings.getPrintPath() + "\\44005001.25759.pdf"); try { ServletOutputStream out = response.getOutputStream(); InputStream in = new ByteArrayInputStream(Files.readAllBytes(path)); byte[] outputByte = new byte[4096]; // copy binary contect to output stream while (in.read(outputByte, 0, 4096) != -1) { out.write(outputByte, 0, 4096); } in.close(); out.flush(); out.close(); } catch (IOException e) { LOG.error("WorkListController.doPrint(): Error while read from file.", e); } finally { }}
Java通過httpResponse spring boot下載文件
慕尼黑5688855
2023-06-14 15:38:40