Servlet從本機讀取一個圖片,並顯示在html頁面
阿新 • • 發佈:2019-01-04
1、工具類
//工具類 package com.JavaBean; import java.awt.image.BufferedImage; import java.io.FileInputStream; import java.io.IOException; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; public class ImgUtil { public static final String JPG = "jpg"; public static final String PNG = "png"; public static final String BMP = "bmp"; public static final String GIF = "gif"; /** * 在servlet中呼叫該方法, jsp頁面中img標籤的src指向該servlet, 則會顯示圖片 * * @param response * @param path * @param isResponseClose */ public static void showImage(HttpServletResponse response, String path, boolean isResponseClose) { try { ServletOutputStream outStream = response.getOutputStream();// 得到向客戶端輸出二進位制資料的物件 FileInputStream fis = new FileInputStream(path); // 以byte流的方式開啟檔案 // 讀資料 byte data[] = new byte[1000]; while (fis.read(data) > 0) { outStream.write(data); } fis.close(); response.setContentType("image/*"); // 設定返回的檔案型別 outStream.write(data); // 輸出資料 if (isResponseClose) { outStream.close(); } } catch (IOException e) { e.printStackTrace(); } } /** * 在servlet中呼叫該方法, jsp頁面中img標籤的src指向該servlet, 則會顯示圖片 * * @param response * @param data * @param isResponseClose */ public static void showImage(HttpServletResponse response, byte[] data, boolean isResponseClose) { try { ServletOutputStream outStream = response.getOutputStream();// 得到向客戶端輸出二進位制資料的物件 // 讀資料 outStream.write(data); response.setContentType("image/*"); // 設定返回的檔案型別 outStream.write(data); // 輸出資料 if (isResponseClose) { outStream.close(); } } catch (IOException e) { e.printStackTrace(); } } /** * 在servlet中呼叫該方法, jsp頁面中img標籤的src指向該servlet, 則會顯示圖片 * * @param response * @param image * @param imgType * @param isResponseClose */ public static void showImage(HttpServletResponse response, BufferedImage image, String imgType, boolean isResponseClose) { try { ImageIO.write(image, imgType, response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } }
2、Servlet
package com.JavBean; import java.io.IOException; import com.JavaBean.ImgUtil; //匯入前面的工具類 import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ImgServlet extends HttpServlet { private static final long serialVersionUID = 3728441236122484282L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String imgPath = request.getParameter("imgPath");// 輸出圖片的型別的標誌 if(null != imgPath && !"".equals(imgPath.trim())) { ImgUtil.showImage(response, imgPath, true); } } }
3、html頁面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>顯示圖片</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body style="overflow-x: hidden;overflow-y: hidden;margin: 0;padding: 0;"> </body> </html> <script> function request(paras){ var url = location.href; var paraString = url.substring(url.indexOf("?")+1,url.length).split("&"); var paraObj = {} for (i=0; j=paraString[i]; i++){ paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1,j.length); } var returnValue = paraObj[paras.toLowerCase()]; if(typeof(returnValue)=="undefined"){ return ""; }else{ return returnValue; } } window.onload=function() { var len=document.documentElement.clientWidth-40; var hit=document.documentElement.clientHeight-10; var addr=request('addr'); document.writeln("<img src='/ImgServlet?imgPath="+addr+"' border=0 width='"+len+"' height='"+hit+"'/>"); } </script>