1. 程式人生 > >JAVA讀取本地檔案並顯示到頁面中

JAVA讀取本地檔案並顯示到頁面中

javaweb圖片的顯示 @RequestMapping(value="showImg") @ResponseBody public void ShowImg(HttpServletRequest request,HttpServletResponse response) throws IOException{ //String imgFile = request.getParameter("imgFile"); //檔名 // String path= UrlUtil.getValue("goodsImg");//這裡是存放圖片的資料夾地址 String path = ConfigUtil.getValue("file_upload_url"); String imgFile = "無標題.png"; FileInputStream fileIs=null; try { fileIs = new FileInputStream(path+"/"+imgFile); } catch (Exception e) { Logger.error(this.getClass(), e); return; } int i=fileIs.available(); //得到檔案大小 byte data[]=new byte[i]; fileIs.read(data); //讀資料 response.setContentType("image.png"); //設定返回的檔案型別 OutputStream outStream=response.getOutputStream(); //得到向客戶端輸出二進位制資料的物件 outStream.write(data); //輸出資料 outStream.flush(); outStream.close(); fileIs.close(); } var src = address + "/basevalue/showImg"; $("#logo").attr("src",src);