2013-08-02 28 views
0

我有這個java servlet Thumbnail.do,它會在您向其發送請求時生成縮略圖圖像。用戶必須通過文件名和寬度用戶要爲圖像。縮略圖程序在Java中,每次頁面刷新時都會給出圖像的隨機圖像

我使用的代碼如下:

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()+"/> 

直到這一切工作正常圖像越來越兌換成該尺寸和頁面上越來越顯示出來。 但問題是在同一頁上假設我打電話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標籤顯示。 任何人都可以建議爲什麼,如果有人知道解決方案的答覆

回答

相關問題