# Java IO 使用 http 輸出圖片 ###### tags: `Java` `IO` ``` File file = new File(path); FileInputStream fin = new FileInputStream(file); ServletOutputStream out = resp.getOutputStream(); // 一個一個讀取 read() 他會回傳下一個的字節data int ch = 0; while ((ch = fin.read()) != -1) { out.write(ch); } out.close(); fin.close(); // 用一個buffer緩衝,read(buf) 把資料放進去buf裡面,並回傳長度 int len = 0; byte[] buf = new byte[1024]; while ((len = fin.read(buf)) != -1) { out.write(buf, 0, len); } out.close(); fin.close(); // 使用BufferedInputStream 跟 BufferedOutputStream 優化 BufferedInputStream bin = new BufferedInputStream(fin); BufferedOutputStream bout = new BufferedOutputStream(out); int len = 0; byte[] bs = new byte[1024]; while ((len = bin.read(bs)) != -1) { bout.write(bs, 0, len); } bin.close(); fin.close(); bout.close(); out.close(); ```