[英]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.