ZK 5.0.6上传

---------ZK 5.0.6上传,upload----------

zul页面

 

 

<button label="上传图片"  id="btnUpload" upload="true,maxsize=-1,native"
							onUpload="win$composer.upload(event.media)"/>

 

 

 Java代码

 

public String upload(Media media) throws IOException {
		String suffix = media.getName().substring(media.getName().lastIndexOf("."));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		picSrc = picPath +"/"+sdf.format(new Date())+suffix;
		InputStream ins = media.getStreamData();
		File f  = new File(Executions.getCurrent().getDesktop().
				getWebApp().getRealPath(picPath)+"/"
				+sdf.format(new Date())+suffix);
		if (!f.exists()) {
			f.createNewFile();
		}
		OutputStream out = new FileOutputStream(f);
		byte[] buf = new byte[1024];
		int len;
		while ((len = ins.read(buf)) > 0) {
			out.write(buf, 0, len);
		}
		out.close();
		ins.close();
		
		return picSrc;
		
	}
 

 

如果用监听:

 

final Button uploadPic = new Button("上传图片");
		uploadPic.setUpload("true,maxsize=-1,native");
		uploadPic.addEventListener(Events.ON_UPLOAD, new EventListener(){
			@Override
			public void onEvent(Event event) throws Exception {
				Media media = ((UploadEvent)event).getMedia();
				info.setPicSrc(upload(media));//upload方法即上面的
			}
		});
 

 

 

你可能感兴趣的:(media,upload,zk,上传)