/** * 文件下載 */ @GetMapping(value = "/download") public void download(HttpServletResponse response) { //文件路徑 try { //文件字節流 JAVA將文件轉換成byte數組(byte[]) 參考:https://www.cnblogs.com/pxblog/p/14919037.html byte[] file = new byte[1024]; response.setContentType("application/vnd.ms-word;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=\"" + new String("文件名包含文件后綴".getBytes("gb2312"), "ISO8859-1")); OutputStream out = response.getOutputStream(); out.write(file); out.flush(); } catch (Exception e) { e.printStackTrace(); } }
如果只是要請求url進行圖片展示,只需要在上面去掉
response.setContentType("application/vnd.ms-word;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=\"" + new String(versionPO.getVersionFileName().getBytes("gb2312"), "ISO8859-1"));
這部分代碼即可