How to upload local file to folder in App Server?
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
(mike_hellsing@yahoo.com)
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?
hi as1225
zk version
zk-3.6.3
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
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
Spam Blocked
Hi javahtml
you can refer to File Upload intro
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。
Hi adez
I think Executions.getCurrent().getDesktop().getWebApp().getRealPath("/dltaxoa-file/other/ ");
It will work
the dltaxoa-file shall under the WebContent
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
You can refer to following doc
http://docs.zkoss.org/wiki/File_Download
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
Hi Hugh,
please refer to Fileupload.setTemplate
How to get file from path without fileupload button. if program running on client machine??
ZK - Open Source Ajax Java Framework
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)