1. 程式人生 > >大小圖生成的兩種方法-未整理

大小圖生成的兩種方法-未整理

圖片上傳都要使用輸入流和輸出流
inputStream is = null
outputSteam os = null
is = file.getInputStream();
儲存檔案資訊
os= fileOutputStream("輸出路徑");

檔案上傳操作
建立位元組陣列
byte[] buff= new byte[1024];
int len= 0;
判斷陣列大小如果比0大的話,大於0的話,說明內容的,
while( (len=is.read(buff))>0){
os.writer(buff);
}

原圖檔案,相對路徑,絕對路徑共三個引數
file.getOriginalFilename();
1.使用一個開源類庫,生成一個縮圖
Thunbnail.of(file.getInputStream()).size(WIDTH,HEIGHI).toFile("伺服器路徑");

2.使用AWT
定義一上輸入流,把檔案輸入服務端
使用Image工具對原圖進行讀入和分析
Image ima = ImageIO.read(file.getInputStream());
獲取原圖的高度
寬度
定義一個縮略比例
寬度和高度

等比縮略,要進行計算,如果完縮比大於高縮比,取大的值
定義一箇中間值wh=0;
if(width >hieght){
wh=with;

}else{
wh=hieght;
}
nWidth = 原圖width/wh;
nHieght = 原圖Hieght/wh;

獲取一個快取圖片物件
bufferedImage buffIma=new BufferedImage(nWidth,nHieght,BufferedImage.Type_int_RGB)

buffIma.getGraphics().drawImage(image.getScaledInstance(nWith,nHieght,image.Scale_Smooth),0,0,null);


獲取一個字尾名//image/jpeg 影象資訊+影象型別
String imgType = file.getContextType().substring(file.getContext().indexOf("/")+1);

ImageIO.write(buffIma,imgType,os);


jsp頁面

<img src=${pageContext.request.contextPath}${imageURL}/>

keepAspectRatio(false)不用進行等比縮略