0

How to upload local file to folder in App Server?

asked 2009-12-03 03:00:37 +0800

mikehellsing gravatar image mikehellsing
42 1 1 1
http://kurohide.wordpress...

updated 2009-12-03 03:49:10 +0800

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)

delete flag offensive retag edit

29 Replies

Sort by ยป oldest newest

answered 2009-12-03 19:23:25 +0800

mikehellsing gravatar image mikehellsing
42 1 1 1
http://kurohide.wordpress...

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

link publish delete flag offensive edit

answered 2009-12-03 19:41:45 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

updated 2009-12-03 19:42:00 +0800

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

link publish delete flag offensive edit

answered 2009-12-04 20:24:23 +0800

mikehellsing gravatar image mikehellsing
42 1 1 1
http://kurohide.wordpress...

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)

link publish delete flag offensive edit

answered 2009-12-06 20:16:10 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

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

	}

}

link publish delete flag offensive edit

answered 2010-03-23 01:39:51 +0800

techvts gravatar image techvts
120 2 5

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

link publish delete flag offensive edit

answered 2010-03-23 09:08:03 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

Hi, techvts
What version of ZK are you using?

link publish delete flag offensive edit

answered 2010-03-23 23:53:46 +0800

techvts gravatar image techvts
120 2 5

hi as1225
zk version
zk-3.6.3

link publish delete flag offensive edit

answered 2010-03-24 02:25:58 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

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

link publish delete flag offensive edit

answered 2010-03-24 02:34:59 +0800

mikehellsing gravatar image mikehellsing
42 1 1 1
http://kurohide.wordpress...

updated 2010-03-24 02:36:26 +0800

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

link publish delete flag offensive edit

answered 2010-03-26 01:31:36 +0800

techvts gravatar image techvts
120 2 5

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

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-12-03 03:00:37 +0800

Seen: 6,395 times

Last updated: Sep 15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More