Java 使用 Zk 框架上传文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19013970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:22:45  来源:igfitidea点击:

Upload files with Zk framework

javagridviewfile-uploaduploadzk

提问by user2768380

I need upload csv files in ZK this is my zul page:

我需要在 ZK 中上传 csv 文件,这是我的 zul 页面:

<zk>
<window
    apply="org.zkoss.bind.BindComposer"
    viewModel="@id('vm') @init('UploadVM')"
    title="win"
    position="center"
    mode="overlapped"
    border="normal"
    width="300px"
    height="200px">
    <button
        label="upload"
        upload="true,maxsize=801192"
        onUpload="@command('uploadFile',upload=event)"
        autodisable="self" />

</window>
</zk>

And my java page:

还有我的 Java 页面:

public class UploadVM {
    private Media media;
    public Media getMedia(){
        return media;
    }
    @NotifyChange("media")
    @Command
    public void uploadFile(@ContextParam(ContextType.TRIGGER_EVENT) UploadEvent event) {
        media = event.getMedia();
        media.getStreamData();
    }

But with this simple example i have the following error:

但是在这个简单的例子中,我有以下错误:

Use getStringData() instead

And i don not know what happen.

我不知道会发生什么。

Who can help me???

谁能帮我???

回答by Eugene Evdokimov

You should know the format of the media before getting data from it. According to the documentationmedia data can be in the binaryor text-basedformat. To retrieve its content you should use getByteData()or getStreamData()in the former case and getStringData()or getReaderData()in the latter. For example,

在从中获取数据之前,您应该了解媒体的格式。根据文档媒体数据可以是binarytext-based格式。要检索其内容,您应该使用getByteData()getStreamData()在前一种情况下和/getStringData()getReaderData()在后一种情况下。例如,

media = event.getMedia();
if (media.isBinary()) {
    InputStream is = media.getStreamData();
} else {
    String s = media.getStringData();
}

回答by Sitansu

I have a best example dropuploadfor uploading csvfile,i thing it will help you for better understanding.

我有一个dropupload上传csv文件的最佳示例,我认为它会帮助您更好地理解。

index.zul

索引.zul

<?page title="Auto Generated index.zul"?>
<window title="Drop here" border="normal" width="100%" height="100%"
    apply="org.zkoss.bind.BindComposer"
    viewModel="@id('vm') @init('com.demo.DropFileViewModel')">

    <dropupload maxsize="5120" detection="none"
        onUpload="@command('doUpload')">
    </dropupload>

    <button label="Download" onClick="@command('doDownload')"></button>


</window> 

DropFileViewModel.java

DropFileViewModel.java

package com.demo;

import org.zkoss.bind.BindContext;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.util.media.Media;
import org.zkoss.zhtml.Filedownload;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zul.Messagebox;

public class DropFileViewModel {
    Media media;

    @Command
    public void doUpload(@ContextParam(ContextType.BIND_CONTEXT) BindContext ctx) {
        UploadEvent upEvent = null;
        Object objUploadEvent = ctx.getTriggerEvent();
        if (objUploadEvent != null && (objUploadEvent instanceof UploadEvent)) {
            upEvent = (UploadEvent) objUploadEvent;
        }
        if (upEvent != null) {
            media = upEvent.getMedia();
            Messagebox.show("File Uploaded: " + media.getName());

        }
    }

    @Command
    public void doDownload() {
        if (media != null)
            Filedownload.save(media);
        else
            Messagebox.show("First Drop Your File");

    }
}

check here

在这里检查

Thanks

谢谢

回答by Klaudiusz Skowronski

It is my solution for generating PDF document:

这是我生成PDF文档的解决方案:

@Command
@NotifyChange("savePDF")
public void savePDF() throws IOException, Exception {

File f = new File("PDF_test.pdf");
OutputStream file = new FileOutputStream(f); 

Document document = new Document();
PdfWriter.getInstance(document, file);

Filedownload.save(f, "application/pdf");

PdfPTable table=new PdfPTable(3);
PdfPCell cell .....
}