2016-02-24 78 views
1

我試圖獲取存儲在我的本地系統目錄圖像如何顯示圖像,我存儲的圖像路徑MySQL數據庫和圖像的路徑是使用<img src=" ">從系統目錄中的JSP

H:\IVS-FEB 2016\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\IVS\uploads\share1.png 

我'm試圖獲取此路徑使用

<img src="<%String pathup =rs2.getString("pathup");out.print(pathup);%>" width="200" height="200" alt="Uploaded by user"> 

但是,這將不會在我的網頁上顯示圖像? :( 我得到了以下錯誤,當我打了檢查瀏覽器

不允許加載本地資源元素選項:文件:/// H:/IVS-FEB%202016/.metadata/.plugins/org。 eclipse.wst.server.core/TMP0/wtpwebapps/IVS /上傳/ share1.png

+0

沒有它的不工作,它仍然不顯示圖像 –

回答

2

撰寫的Java Servlet見example tutorial

例如:。

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{ 
    ServletOutputStream oStream; 
    String fileName = "your file"; 
    try (FileInputStream iStream = new FileInputStream(new File(fileName))) 
    { 
     response.setContentType("image/png"); 
     oStream = response.getOutputStream(); 

     byte[] buffer = new byte[1024]; 
     int len; 
     while ((len = iStream.read(buffer)) != -1) 
     { oStream.write(buffer, 0, len); } 

    } 

    oStream.flush(); 
    oStream.close(); 
} 

然後在你的HTML/JSP頁面使用:

<img src="ImageServlet"/> 

可以傳遞參數,如果您有基於任何條件下多張圖像,做你的邏輯在Servlet類選擇。