瀏覽器下載圖片
阿新 • • 發佈:2018-12-20
/** * 下載圖片 * @param request * @throws IOException */ @ResponseBody @RequestMapping("downLoad.do") public void downLoad(HttpServletRequest request, HttpServletResponse response) throws IOException { String url = request.getParameter("pictureUrl"); String picName = url.substring(url.lastIndexOf("/")+1); String[] split = picName.split("\\."); //獲取檔案 File file = new File(url); //將圖片讀成二進位制流 FileImageInputStream fs = new FileImageInputStream (file); int streamLength = (int)fs.length(); byte[] image = new byte[streamLength ]; fs.read(image,0,streamLength ); fs.close(); response.setHeader("Content-Type","application/octet-stream"); response.setHeader("Content-Disposition","attachment;filename="+split[0]+"."+split[1]); response.getOutputStream().write(image); response.getOutputStream().flush(); response.getOutputStream().close(); }