简体   繁体   English

Java中的缩略图程序每次刷新页面时都会为图像提供随机图像

[英]Thumbnail Program In Java everytime on page refresh it gives random images for a image

I have this java servlet Thumbnail.do which generates a thumbnail image everytime you send a request to it .User has to pass the file name and width the user wants for the image. 我有这个Java servlet Thumbnail.do,每次您向它发送请求时都会生成一个缩略图图像。用户必须传递用户想要的图像文件名和宽度。

The code I am using is below: 我正在使用的代码如下:

public String Image="",ImgWidth="";


Image= "d:\\app\\project\\media\\"+req.getParameter("image");
ImgWidth= req.getParameter("width");
BufferedImage  bufferedimage =ImageIO.read(new File(Image))
float scale=1;
int targetWidth=0;
int targetHeight=0;
Imgwidth=req.getParameter("width");
targetWidth=(int)(bufferedimage.getWidth(null)* scale);
targetHeight=(int)(bufferedimage.getHeight(null)* scale);
if(ImgWitdh == null || ImgWitdh.equlas("")){
ImgWitdh ="0";

}
if(targetWidth>Integer.parseInt(ImgWitdh)&& !ImgWitdh.equals("0")){
targetHeight=Integer.parseInt(ImgWitdh) * targetHeight/targetWidth;
targetWidth=Integer.parseInt(ImgWitdh);
} 

ImageIO.write(createResizedCopy(bufferedimage,targetWidth,
targetHeight,imageOutput,
res.getOutputStream());



 BufferedImage createResizedCopy(Image originalImage, int scaledWidth, int       
 scaledHeight) 
{
 BufferedImage  bufferedimage =new BufferedImage(scaledWidth, scaledHeight,  
 BufferedImage.TYPE_INT_RGB );

 Graphics2D g = scaledBI.createGraphics();
 g.setComposite(AlphaComposite.Src);
 g.drawImage(originalImage,0,0,scaledWidth,scaledHeight,null);
 g.dispose();
}

And on whichever page I have to display the image ,I call the servlet Like this 在我必须显示图像的任何页面上,我都会像这样调用servlet

<img src="../Thumbnail.do?image="the_image_name"&width=150&target="+Math.random()+"/>

till this everything works fine the image are getting converted to the said size and are getting displayed on the page . 直到一切正常,图像转换为所述尺寸并显示在页面上。 But the problem is Suppose on the same page I am calling Thumbnail.do multiple times to display different images at various locations on the page like 但是问题是,假设我在同一页面上多次调用Thumbnail.do,以在页面的各个位置显示不同的图像,例如

 <div>
 <img src="../Thumbnail.do?image="emp.png"&width=150&target="+Math.random()+"/>
 </div>
 <div>
 <img src="../Thumbnail.do?image="logo.png"&width=50&target="+Math.random()+"/>
 </div.

then what happens is every time I refresh the page random images are displayed in the div tags. 那么会发生什么,就是每次我刷新页面时,div标签中都会显示随机图像。 can anyone suggest why and if anyone knows the solution reply 任何人都可以建议原因,以及是否有人知道解决方案的答复

If I understand your question correctly, the problem is the browser caches the image from your servlet. 如果我正确理解了您的问题,则问题是浏览器从servlet缓存了图像。 You can disable caching in your servlet code using the approaches described in the link: How to prevent the result of Servlets from being cached 您可以使用链接中描述的方法禁用Servlet代码中的缓存: 如何防止Servlet的结果被缓存

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

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