ZK - Open Source Ajax Java FrameworkZK - Open Source Ajax Java Framework

How to upload local file to folder in App Server?

mikehellsing
3 Dec 2009 03:00:37 GMT
3 Dec 2009 03:00:37 GMT

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
(mike_hellsing@yahoo.com)

mikehellsing
3 Dec 2009 19:23:25 GMT
3 Dec 2009 19:23:25 GMT

hello..anyone have same experience? suggest or solution? needing help plz.. Thanks

jimmyshiau
3 Dec 2009 19:41:45 GMT
3 Dec 2009 19:41:45 GMT

Hi,mikehellsing
you can call media.getStreamData() to get InputStream
and write it to where you want

mikehellsing
4 Dec 2009 20:24:23 GMT
4 Dec 2009 20:24:23 GMT

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
(mike_hellsing@yahoo.com)

jimmyshiau
6 Dec 2009 20:16:10 GMT
6 Dec 2009 20:16:10 GMT

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);
			}
		}

	}

}

techvts
23 Mar 2010 01:39:51 GMT
23 Mar 2010 01:39:51 GMT

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

jimmyshiau
23 Mar 2010 09:08:03 GMT
23 Mar 2010 09:08:03 GMT

Hi, techvts
What version of ZK are you using?

techvts
23 Mar 2010 23:53:46 GMT
23 Mar 2010 23:53:46 GMT

hi as1225
zk version
zk-3.6.3

jimmyshiau
24 Mar 2010 02:25:58 GMT
24 Mar 2010 02:25:58 GMT

I test my sample on FF 36 with ZK 3.6.3 win7
it works well

mikehellsing
24 Mar 2010 02:34:59 GMT
24 Mar 2010 02:34:59 GMT

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

techvts
26 Mar 2010 01:31:36 GMT
26 Mar 2010 01:31:36 GMT

hi ,

i don't know why it don't work for me i tried on zk 3.6.2 and zk 5 as well

do we need make any changes in zk.xml

plz help
thanks

javahtml
13 Aug 2010 15:13:29 GMT
13 Aug 2010 15:13:29 GMT

Spam Blocked

jimmyshiau
15 Aug 2010 21:32:26 GMT
15 Aug 2010 21:32:26 GMT

Hi javahtml
you can refer to File Upload intro

adez
2 Sep 2010 01:58:49 GMT
2 Sep 2010 01:58:49 GMT

hi everybody.i need the SAVE_PATH flom Service,not like you code SAVE_PATH = "c:\\myfile\\media\\";
I used to modify this code "Object contextPath = Executions.getCurrent().getDesktop().getWebApp().getRealPath("/dltaxoa-file/other/ /"); "But the wrong,can you tell me well do?
Thank you very much。

jimmyshiau
5 Sep 2010 19:55:28 GMT
5 Sep 2010 19:55:28 GMT

Hi adez
I think Executions.getCurrent().getDesktop().getWebApp().getRealPath("/dltaxoa-file/other/ ");
It will work
the dltaxoa-file shall under the WebContent

adez
11 Sep 2010 21:06:15 GMT
11 Sep 2010 21:06:15 GMT

hi as1225
the code Executions.getCurrent().getDesktop().getWebApp().getRealPath("/dltaxoa-file/other/ ");
it work will for Uploading Files;
But when I use to download files it wrong;the download files path was changed
my download code:
//download
public void onFiledown() throws IOException
{
//取出数据库中的完整路径
String path = availableToolInfo.getAccessory().toString();
//转换成相对路径
String temp1 = path.substring(0,path.lastIndexOf("\\"));
String temp2 = temp1.substring(0,temp1.lastIndexOf("\\"));
Filedownload.save("temp2",null ,null);

}

can you give me a code for download;
Thanks again

jimmyshiau
13 Sep 2010 20:43:59 GMT
13 Sep 2010 20:43:59 GMT

You can refer to following doc
http://docs.zkoss.org/wiki/File_Download

shuggy
22 Feb 2011 16:17:25 GMT
22 Feb 2011 16:17:25 GMT

Thanks so much for this helpful article. I have it working perfectly in my app. One question, is there a way to specify the effect of the progress of the file upload ? I would prefer to use the standard progress bar rather than the upload pop up where the user needs the press browse, then upload.

Thanks
Hugh

jimmyshiau
22 Feb 2011 18:46:23 GMT
22 Feb 2011 18:46:23 GMT

Hi Hugh,
please refer to Fileupload.setTemplate

rohitwadke
9 Mar 2011 22:47:57 GMT
9 Mar 2011 22:47:57 GMT

How to get file from path without fileupload button. if program running on client machine??

1 2