-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Dear,
I'm new developing Java web project using Zk 3.6.2, found some problem while want add some upload file feature in my app. I use script :
Media media = Fileupload.get();
to get the file (image file) from client local folder then store to Object media. how to manipulate (ex: copy or rename) this local file
to folder in my App Server, using the Zk component? where I want specify manually the App Server folder path to store the upload file in my code..
I've search for this forum n web before, but found nothing.. Thanks before for answer n solution.
Regards,
Maikel
([email protected])
hello..anyone have same experience? suggest or solution? needing help plz.. Thanks
Hi,mikehellsing
you can call media.getStreamData() to get InputStream
and write it to where you want
Hi as1225
I've tried using media.getStreamData() as your suggestion to have an Object InputStrem, then i use it to general simple copy file code to try:
Media media = Fileupload.get();
OutputStream out = new FileOutputStream("D:/" + media.getName());
byte[] buf = new byte[1024];
int len;
while ((len = media.getStreamData().read(buf)) > 0) {
out.write(buf, 0, len);
}
media.getStreamData().close();
out.close();
But seem the destination copy file looping without end (i stop the apps server manually n got the 7,5GB big size file ^^)
sorry, i still new in Java too..can you give me some sample code n How to use/implements it?
Thanks before,
Maikel
([email protected])
uploadPage.zul
<zk> <window apply="ctrl.UploadCtrl"> <button id="uploadBtn" label="Upload file" /> <separator /> <image id="img" /> <separator /> <label id="msgLb" /> </window> </zk>
UploadCtrl,java
package ctrl; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.zkoss.util.media.Media; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.util.GenericForwardComposer; import org.zkoss.zul.*; public class UploadCtrl extends GenericForwardComposer { private Image img; private Label msgLb; private static final int FILE_SIZE = 100;// 100k private static final String SAVE_PATH = "c:\\myfile\\media\\"; @Override public void doAfterCompose(Component comp) throws Exception { super.doAfterCompose(comp); } public void onClick$uploadBtn() { msgLb.setValue(""); try { Media media = Fileupload.get(); if(media == null){ msgLb.setValue("please select a file"); return; } String type = media.getContentType().split("/")[0]; if (type.equals("image")) { if (media.getByteData().length > FILE_SIZE * 1024) { msgLb.setValue("File size limit " + FILE_SIZE + "k"); return; } org.zkoss.image.Image picture = (org.zkoss.image.Image) media; img.setContent(picture); } saveFile(media); } catch (InterruptedException e) { e.printStackTrace(); } } private void saveFile(Media media) { BufferedInputStream in = null; BufferedOutputStream out = null; try { InputStream fin = media.getStreamData(); in = new BufferedInputStream(fin); File baseDir = new File(SAVE_PATH); if (!baseDir.exists()) { baseDir.mkdirs(); } File file = new File(SAVE_PATH + media.getName()); OutputStream fout = new FileOutputStream(file); out = new BufferedOutputStream(fout); byte buffer[] = new byte[1024]; int ch = in.read(buffer); while (ch != -1) { out.write(buffer, 0, ch); ch = in.read(buffer); } msgLb.setValue("sucessed upload :" + media.getName()); } catch (IOException e) { throw new RuntimeException(e); } catch (Exception e) { throw new RuntimeException(e); } finally { try { if (out != null) out.close(); if (in != null) in.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }
i tried u r code.
Media media = Fileupload.get();
if(media == null){
msgLb.setValue("please select a file");
return;
}
i doesn't work for me because media=null comes though i select a file why i couldn't understand plz help
Hi, techvts
What version of ZK are you using?
I test my sample on FF 36 with ZK 3.6.3 win7
it works well
Hi techvts,
I've tried as1225 sample code couple months ago while using Zk 3.6.2, now with Zk 5 CE, both works well. my problem solve :D
Asked: 2009-12-03 03:00:37 +0800
Seen: 6,482 times
Last updated: Sep 15 '17