1. 程式人生 > >JAVA 檔案下載 通過controller 轉到指定地址

JAVA 檔案下載 通過controller 轉到指定地址

@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(); }