zk框架中java有关后台图片上传及尺寸控制
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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。