2018年6月21日 星期四

JSP/Servlet 圖片顯示

1.網頁直接瀏覽圖片的寫法

HttpServletResponse  response = this.getResponse(); 
File imageFile = new File("C://image.jpg");
try {
 ServletOutputStream myOut = response.getOutputStream();
 FileInputStream input = new FileInputStream(imageFile);
 BufferedInputStream buf = new BufferedInputStream(input);
 response.setContentType("image/jpeg");
 response.addHeader("Content-Disposition","inline; filename="+name);
 int readBytes = 0;
 while ((readBytes = buf.read()) != -1) {
  myOut.write(readBytes);
 }
 buf.close();
 input.close();
 myOut.close();
}catch (Exception e) {
 // TODO: handle exception
 e.printStackTrace();
}