0

リクエストを送信するたびにサムネイル画像を生成するこのJavaサーブレットThumbnail.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();
}

そして、画像を表示する必要があるページで、このようにサーブレットを呼び出します

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

これがすべて正常に機能するまで、画像は上記のサイズに変換され、ページに表示されます。しかし問題は、同じページで 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.

次に、ページを更新するたびに、ランダムな画像が div タグに表示されます。誰かが理由を提案できますか、そして誰かが解決策を知っている場合は返信できますか

4

1 に答える 1

1

あなたの質問を正しく理解できれば、問題はブラウザがサーブレットからの画像をキャッシュすることです。リンクで説明されている方法を使用して、サーブレット コードでキャッシュを無効にすることができます:サーブレットの結果がキャッシュされないようにする方法

于 2013-08-02T13:04:06.063 に答える