简体   繁体   English

图像需要花费大量时间才能加载到JSP中

[英]Images take a lot of time to load in JSP

I have a scenario where I am fetching multiple images from the Oracle database through the loop. 我有一个场景,我通过循环从Oracle数据库中获取多个图像。 But it's taking a lot of time to fetch the images and display it on the browser. 但它需要花费大量时间来获取图像并将其显示在浏览器上。 Through, this code I am calling my method with some parameters. 通过,这段代码我用一些参数调用我的方法。

for(Object[] obj: memberDetails)
{
    System.out.println("String.valueOf(obj[0])"+String.valueOf(obj[0]));

    try{
        memberImage=dtSrvc.getQueImageForQC(Id,String.valueOf(obj[0]));
    }
    catch(Exception e)
    {}

    map.put("memImage"+count, memberImage);
    key.add("memImage"+count);
    hmap.put("memImage"+count, memberImage);
    count++;
}  

and here is my query through which I am fetching images 这是我提取图像的查询

ps = conn.prepareStatement("select photo from member_photo where ID='"
                        + Id + "' and que_id=" + QueId);

Please, let me know how can I speed up this process. 请让我知道如何加快这个过程。 I am unable to fetch all images if the network is slow. 如果网络速度慢,我无法获取所有图像。 My application is currently used by many peoples. 我的申请目前被许多人使用。

The problem is that you use individual query to fetch each image from the db. 问题是您使用单个查询从数据库中获取每个图像。 It makes a network overhead. 它会产生网络开销。 You can try many of the solutions described here to change your code to do one query instead of many. 您可以尝试此处描述的许多解决方案来更改代码以执行一个查询而不是多个查询。 See how to Batching Select Statements in JDBC . 了解如何在JDBC中批处理选择语句

ps = conn.prepareStatement("select photo from member_photo where ID in (" + inClause.toString() + ')');

you don't need que_id if ID is a primary key in the table. 如果ID是表中的主键,则不需要que_id

My suggestion is about fetching images first and keep them as pure file(temp) on a accessible and public folder of your application, and then simply point them out with simple image resource using img tag. 我的建议是首先获取图像并将它们保存为应用程序的可访问和公共文件夹中的纯文件(temp),然后使用img标记简单地将它们指向简单的图像资源。
The process would be like this. 这个过程就是这样的。
1. Query images and keep them under /a_public_folder/images/... which each images is going be named as 1.jpeg , 2.jpeg where 1 and 2 are ids(unique). 1.查询图像并将它们保存在/a_public_folder/images/... ,每个图像将被命名为1.jpeg2.jpeg ,其中1和2是ids(唯一)。
2. keep the ids retrieved from the database with a simple context such as list_id:List<Long> 2.使用简单的上下文(例如list_id:List<Long>保留从数据库中检索的ID
3. your final servlet would be like the following 3.你的最终servlet将如下所示

for(long idx:list_id){
  out.print("<img src=\"/a_public_folder/images/"+idx+".jpeg\"/>");
}

4. you may also apply a filter for /a_public_folder/images/* path in order to just allow users who have permission to view content/images 4.您也可以为/a_public_folder/images/*路径应用过滤器,以便只允许有权查看内容/图像的用户
5. have a session listener in order to remove associated(fetched) images to the particular user session, or have a daemon thread for cleaning up the directory(images have no links longer) interval. 5.有一个会话监听器,以便将关联(提取)的图像移除到特定的用户会话,或者有一个守护程序线程来清理目录(图像没有更长的链接)间隔。

But I suggest extract images from database to simple pure file and just keep the images path into the database. 但我建议从数据库中提取图像到简单的纯文件,并将图像路径保存到数据库中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM