標籤:java zk 圖片
private class ImageDeal implements EventListener {
private int type;
public ImageDeal(int type) {
this.type = type;
}
public void onEvent(Event arg0) throws Exception {
switch (type) {
case 1:
if (arg0 instanceof UploadEvent) {
UploadEvent _eve = (UploadEvent) arg0;
Media media = _eve.getMedia();
if (null != media) {
if (media instanceof org.zkoss.image.Image) {
Desktop des = Executions.getCurrent().getDesktop();
String name = des.getSession().getWebApp()
.getRealPath("");
name += File.separator + "imgs" + File.separator;
// name += File.separator + "imgs" + File.separator
// + "255px" + File.separator;
String[] _tmp = media.getName().split("\\.");
String fileName = ppt.getId() + "."
+ _tmp[_tmp.length - 1];
File file = new File(name + fileName);
if (file.exists())
file.delete();
Files.copy(file, media.getStreamData());
Files.close(media.getStreamData());
//ppt.setExp2(fileName);
String destPath=name +"255px"+File.separator+fileName;
File destFile = new File(destPath);
//圖片壓縮處理
ImageUtils image = new ImageUtils(file, destFile);
image.smallImg(255,178);
//showSlt();顯示圖片
} else {
//自己編輯windows.showMess("您上傳的不是圖片類型的檔案,請重試");
}
}
}
break;
}
}
}
//本人項目中顯示圖片的方法
private void showSlt() {
Image tp = (Image) this.getFellowIfAny("tp");//zk組件
tp.getChildren().clear();
if (!TextUtils.isEmpty(ppt.getExp2())) {
tp.setSrc("imgs/255px/" + ppt.getExp2());
}
}
壓縮處理圖片的工具類
private void showSlt() {
Image tp = (Image) this.getFellowIfAny("tp");
tp.getChildren().clear();
if (!TextUtils.isEmpty(ppt.getExp2())) {
tp.setSrc("imgs/255px/" + ppt.getExp2());
}
}
本文出自 “本源” 部落格,請務必保留此出處http://benyuan.blog.51cto.com/8521942/1637234
zk架構中java有關後台圖片上傳及尺寸控制