簡體   English   中英

圖像Servlet無法正常工作

[英]Image Servlet not working

嘿,有人可以用我的servlet幫助我嗎,該servlet是在從數據庫中獲取圖像后將圖像顯示到我的jsp頁面上的。 我下面的圖像servlet似乎根本沒有在瀏覽器中顯示圖像,只是一個損壞的圖像圖標:

@WebServlet(name = "ImageServlet", urlPatterns = {"/Image"})
public class ImageServlet extends HttpServlet {

private DatabaseConnector dataManager;
BufferedInputStream input = null;
BufferedOutputStream output = null;

@Override
public void init() {
    dataManager = new DatabaseConnector();
}

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Prepare.
    String name = request.getPathInfo().substring(1); // Returns "foo.gif". You may want to do nullchecks here to avoid NPE's.
    ResultSet resultSet = null;

    try {
        // Query DB.
        resultSet = dataManager.getPhotos(1);

        if (resultSet.next()) {
            // Image found, prepare response and send it.
            response.setContentType(resultSet.getString("contentType"));
            response.setContentLength(resultSet.getInt("contentLength"));
            response.setHeader("Content-Disposition", "inline;filename=\"" + name + "\"");
            input = null;
            output = null;

            try {
                input = new BufferedInputStream(resultSet.getBinaryStream("content"));
                output = new BufferedOutputStream(response.getOutputStream());
                byte[] buffer = new byte[1024];

                for (int length; (length = input.read(buffer)) > -1;) {
                    output.write(buffer, 0, length);
                }
            } finally {
                if (output != null) {
                    try {
                        output.close();
                    } catch (IOException logOrIgnore) {
                    }
                }
                if (input != null) {
                    try {
                        input.close();
                    } catch (IOException logOrIgnore) {
                    }
                }
            }
        } else {
            // No image found, send HTTP 404.
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(ImageServlet.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalArgumentException ex) {
        Logger.getLogger(ImageServlet.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException e) {
        throw new ServletException("Something failed at SQL/DB level.", e);
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException logOrIgnore) {
            }
        }
    }
}
}

我的jsp頁面中的圖像如下所示:

<img src="/Image/<%= h.getHomeID() %>">

最后,我來自結果集的數據庫連接器類是:

public ResultSet getPhotos(String photoID) throws
        IllegalArgumentException, SQLException, ClassNotFoundException {

    createConnection();


    ResultSet rs = null;
    PreparedStatement preparedStatement = null;

    String strQuery = "SELECT home_photo.photo "
            + "FROM home_photo "
            + "WHERE home_photo.home_id = ?";

    try {
        preparedStatement = conn.prepareStatement(strQuery);
        preparedStatement.setString(1, photoID);
        rs = preparedStatement.executeQuery();

    } catch (SQLException e) {
        throw new SQLException(e);
    } finally {
        closeConnection();
        return rs;
    }
}

我的web.xml也看起來像這樣:

<servlet>
    <servlet-name>ImageServlet</servlet-name>
    <servlet-class>DB.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ImageServlet</servlet-name>
    <url-pattern>/Image/*</url-pattern>
</servlet-mapping>

如果有人知道為什么不顯示圖像,我將很感激

有兩個潛在的問題。

  1. 圖片網址錯誤。 在您的Web瀏覽器開發人員工具集的HTTP流量監視器中查看(按Chrome / IE9 / Firebug中的F12,然后查看“ 網絡(工作)”部分)。 HTTP響應代碼必須為2nn / 3nn,而不是404。adarshr的答案涵蓋了這一點。

     <img src="${pageContext.request.contextPath}/Image/<%= h.getHomeID() %>"> 

    也可以看看:


  2. 您將在返回結果集之前關閉數據庫連接,但是在此之后仍然嘗試訪問結果集。 這將引發SQLException ,該異常應顯示為HTTP 500錯誤頁面。 在HTTP流量監控器中,甚至在瀏覽器的地址欄中直接指定圖像的URL時,也應該可見。

    您需要重寫代碼,而不返回ResultSet ,而只是將圖像的內容作為byte[] ,或者將DB作業移至Servlet內部。 無論如何,永遠不要創建采用或返回ResultSet實例的公共方法。

    也可以看看:

看起來圖像servlet的URL錯誤。 如果以/開頭,它將帶您到上下文的根。

<img src="${pageContext.request.contextPath}/Image/<%= h.getHomeID() %>">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM