嗨,我需要JSP页面中的流式图像帮助

时间:2012-03-02 02:39:30

标签: java image jsp servlets outputstream

好的,所以我创建了一个上传OGG音乐的音乐上传网站。它还包含一个音频标签。我还将专辑封面作为字符串放入我的数据库中。

现在,我想在我的JSP中显示该字符串(代表我的专辑封面):

@WebServlet(name = "LoadAlbumArt", urlPatterns = { "/LoadAlbumArt" })
public class LoadAlbumArt extends HttpServlet {
   /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    * @throws ServletException if a servlet-specific error occurs
    * @throws IOException if an I/O error occurs
    */
   protected void processRequest(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
      response.setContentType("image/jpg");
      try {
         OutputStream outputStream = response.getOutputStream();
         DBConnector bConnector = new DBConnector();
         PreparedStatement preparedStatement = bConnector
               .Connect("SELECT * FROM devwebmp3.musicdatabase where musicno = ?");
         preparedStatement.setInt(1,
               Integer.parseInt(request.getParameter("musicno")));
         ResultSet resultSet = preparedStatement.executeQuery();
         Blob blob = null;
         String imagestring = null;
         while (resultSet.next()) {
            imagestring = resultSet.getString("albumart");
         }

         //BufferedImage bi = ImageIO.read(ImageIO.createImageInputStream(new ByteArrayInputStream(Base64Coder.decode(imagestring.toCharArray()))));

         //outputStream.write(blob.getBytes(1, (int) blob.length()));
         byte[] hello = Base64Coder.decode(imagestring);
         //ImageIO.write(bi, "jpg", outputStream);
         //System.out.println("byte" + hello);
         outputStream.write(hello);
         outputStream.flush();
         outputStream.close();
      } catch (Exception e) {
         // ...
      }

      // ...
   }
}

此外,这是java servlet页面:

src=<%="\"LoadAlbumArt?musicno="+request.getParameter("musicno") +"\""%>>

1 个答案:

答案 0 :(得分:0)

首先,您在哪里调用此processRequest(..)方法?

您确定在该servlet的processRequest(..)方法中包含doGet(..)的调用,如下所示:

public void doGet(HttpServletRequest req, HttpServletResponse resp) 
                                   throws ServletException, IOException {
    processRequest(req,resp);
}

您是否通过请求

检查已知记录的输出
http://.../LoadAlbumArt?musicno=1 

您的Servlet是否正确响应了JPEG图像?如果没有,那么你应该检查你的Servlet代码。

同时将您的视图页面中的表达式更改为:

<img src="/LoadAlbumArt?musicno=${param.musicno}" />

那些JSP小脚本和表达式(<% %><%= %>)现在是古董遗物,除非你有一些旧的代码要复活,否则你永远不应该使用它们。

您没有提供有关数据库表,BLOB字段的足够详细信息,即使您的问题中有随机注释的代码,也很难决定您是否使用它们。