简体   繁体   English

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

[英]Hi I need help in streaming image in my JSP page

Okay, so I have created a music uploading website that uploads OGG music. 好的,所以我创建了一个音乐上传网站,可以上传OGG音乐。 It also has an audio tagger incorporated. 它还集成了音频标记器。 I also put the album art into my database as a string. 我还将专辑封面作为字符串放入到数据库中。

Now, I want to display that string (representing my album art) to my JSP: 现在,我想在我的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) {
         // ...
      }

      // ...
   }
}

In addition, this is the java servlet page: 另外,这是java servlet页面:

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

First of all, where do you call this processRequest(..) method? 首先,在哪里调用此processRequest(..)方法?

Are you sure that you included a call for processRequest(..) in that servlet's doGet(..) method like this: 您确定在该servlet的doGet(..)方法中包括对processRequest(..)的调用,如下所示:

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

Did you check the output of a known record by requesting 您是否通过请求检查了已知记录的输出

http://.../LoadAlbumArt?musicno=1 

Does your Servlet properly response with a JPEG image? 您的Servlet是否正确响应JPEG图像? If not, then you should check your Servlet code. 如果不是,则应检查Servlet代码。

Also change your expression in your View page to this: 另外,将“视图”页面中的表达式更改为此:

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

Those JSP scriptlets and expressions ( <% %> and <%= %> ) are antique relics now, you should NEVER use them unless you have some old code to resurrect. 那些JSP脚本和表达式( <% %><%= %> )现在是古董,您除非有一些旧代码可以复活,否则永远不要使用它们。

You didn't give enough details about your database table, BLOB field, even there are random commented code in your question which is hard to decide whether you used them or not. 您没有提供有关数据库表BLOB字段的足够详细信息,即使您的问题中包含随机注释的代码,也很难决定是否使用它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要帮助将我的一个JSP页面重定向到另一个JSP的文本框中的预先填充的值 - Need help to redirect my one JSP page with prepopulated value in my textbox in another JSP 嗨,我需要帮助来解析Java中的JSON文件 - Hi, I need help parse through a JSON file in Java 您好,我的孩子jsp中有一个名为DECISION的字段,如何将这个值传递给父jsp - hi i have a field called DECISION in my child jsp how can i get that value to parent jsp 我的程序需要帮助 - I need help in my program 嗨,我正在为登录表单做下面的Java代码,它没有将我重定向到employee.jsp或customer jsp页面 - Hi, I am doing the java code found below, for a login form.It is not redirecting me to the employee.jsp or customer jsp page 需要帮助使用油漆显示我的图像 - Need help displaying my Image using paint 我需要帮助在屏幕上移动图像 - I Need to help to move an image across the screen 我需要帮助将图像插入 java 中的 JPanel, - I need help inserting an image to a JPanel in java, 我想显示welcome.jsp是我在项目中的初始页面,但是它没有出现,因此任何人都可以发现错误并帮助我 - i want to show the welcome.jsp is my initial page in the project but it not coming so any one find the mistakes and help me 我需要帮助将图像存储在内部目录中,并且其路径已插入SQLite中 - I need help to store my image in internal directory and its path is insert in SQLite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM