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

Java中的缩略图程序每次在页面刷新时都会为一个图像提供随机图像,java,Java,我有一个JavaServletThumbnail.do,每当您向它发送请求时,它都会生成一个缩略图。用户必须传递用户想要的图像的文件名和宽度 我使用的代码如下: public String Image="",ImgWidth=""; Image= "d:\\app\\project\\media\\"+req.getParameter("image"); ImgWidth= req.getParameter("width"); BufferedImage bufferedimage =Im

我有一个JavaServletThumbnail.do,每当您向它发送请求时,它都会生成一个缩略图。用户必须传递用户想要的图像的文件名和宽度

我使用的代码如下:

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();
}
在任何需要显示图像的页面上,我都像这样调用servlet

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

在一切正常之前,图像将转换为所述大小并显示在页面上。 但问题是,假设在同一页上我调用缩略图。请多次在页面的不同位置显示不同的图像 像



如果我正确理解了您的问题,问题是浏览器缓存了servlet中的图像。您可以使用链接中描述的方法禁用servlet代码中的缓存:

 <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.