JAVA 檔案下載 通過controller 轉到指定地址
阿新 • • 發佈:2019-02-15
@ResponseBody @RequestMapping(value = "/DownLoadFile") public void downLoadFile(String Id,HttpServletRequest request, HttpServletResponse response) throws Exception { CloseableHttpClient client = HttpClientBuilder.create().build();HttpGet get = new HttpGet("URL"); String fileName = "baidu.jpg"; String contentType = request.getSession().getServletContext().getMimeType(fileName); HttpResponse resp = client.execute(get); InputStream is = resp.getEntity().getContent(); response.setContentType(contentType); response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\""); OutputStream out = response.getOutputStream(); byte[] bytes = IOUtils.toByteArray(is); out.write(bytes); out.flush(); out.close(); is.close(); get.releaseConnection(); }