Vaadin Web应用开发教程(22):UI组件-Upload组件

十一、Vben框架部分组件样式的重新封装 Vben框架部分组件样式的重新封装,如inputNumber带前后标志,select组件增加label返回 阅读详情

Upload 组件用于向服务器上传文件。它显示一个文件名输入框,一个文件选择按钮和一个上传确认按钮。

 


可以通过setButtonCaption 修改”Upload” 按钮的文字。对于”Browser” 按钮由于浏览器安全方面的考虑,难以修改它的外观,”Brower” 显示语言取决于浏览器本身。 因此如果你想保持”Upload” 语言显示的一致,你必须使用和“Browser”一样的语言。
通常情况上传的文件可以存放在文件系统,数据库或是临时存放在内存中,Upload 组件将上传的文件数据写到一个java.io.OutputStream 对象中,因此你可以使用你喜欢的方法来处理上传到服务器的文件。
使用Upload 组件,需要实现Upload.Receiver 接口来处理文件数据,它将在用户点击”upload”按钮后调用。
当文件上传结束后,成功与否Upload组件将触发Upload.FinishedEvent 事件。可以通过Upload.FinishedListener 接口来处理这个事件,事件参数包括文件名,MIME类型和文件长度。
下面的例子将上传的图像文件存放在/tmp/uploads 目录下 并显示最后上传的图像。

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.vaadin.terminal.FileResource;
import com.vaadin.ui.*;

public class MyUploader extends CustomComponent
                        implements Upload.SucceededListener,
                                   Upload.FailedListener,
                                   Upload.Receiver {

    Panel root;         // Root element for contained components.
    Panel imagePanel;   // Panel that contains the uploaded image.
    File  file;         // File to write to.

    MyUploader() {
        root = new Panel("My Upload Component");
        setCompositionRoot(root);

        // Create the Upload component.
        final Upload upload =
                new Upload("Upload the file here", this);

        // Use a custom button caption instead of plain "Upload".
        upload.setButtonCaption("Upload Now");

        // Listen for events regarding the success of upload.
        upload.addListener((Upload.SucceededListener) this);
        upload.addListener((Upload.FailedListener) this);

        root.addComponent(upload);
        root.addComponent(new Label("Click 'Browse' to "+
                "select a file and then click 'Upload'."));

        // Create a panel for displaying the uploaded image.
        imagePanel = new Panel("Uploaded image");
        imagePanel.addComponent(
                         new Label("No image uploaded yet"));
        root.addComponent(imagePanel);
    }

    // Callback method to begin receiving the upload.
    public OutputStream receiveUpload(String filename,
                                      String MIMEType) {
        FileOutputStream fos = null; // Output stream to write to
        file = new File("/tmp/uploads/" + filename);
        try {
            // Open the file for writing.
            fos = new FileOutputStream(file);
        } catch (final java.io.FileNotFoundException e) {
            // Error while opening the file. Not reported here.
            e.printStackTrace();
            return null;
        }

        return fos; // Return the output stream to write to
    }

    // This is called if the upload is finished.
    public void uploadSucceeded(Upload.SucceededEvent event) {
        // Log the upload on screen.
        root.addComponent(new Label("File " + event.getFilename()
                + " of type '" + event.getMIMEType()
                + "' uploaded."));
        
        // Display the uploaded file in the image panel.
        final FileResource imageResource =
                new FileResource(file, getApplication());
        imagePanel.removeAllComponents();
        imagePanel.addComponent(new Embedded("", imageResource));
    }

    // This is called if the upload fails.
    public void uploadFailed(Upload.FailedEvent event) {
        // Log the failure on screen.
        root.addComponent(new Label("Uploading "
                + event.getFilename() + " of type '"
                + event.getMIMEType() + "' failed."));
    }
}

 


 

 

五、vben的上传和下载 vben的上传和下载,适用于vue框架 阅读详情

相关推荐

Vaadin介绍与开发练习之一(总体介绍与创建项目)

Vaadin是什么? Vaadin是一个web应用程式开发框架。基于这个框架,开发者可以使用Java开发高质量的用户界面。 Vaadin由位于芬兰的 IT Mill公司开发。 其前身是 IT Mill 公司由2000年起开始研发的 Millstone用户界面类库。 Vaadin提供了一系列的用户界面组件用来创建自己的组件。特点主要包含:易用、可重用、可扩展和满足大型企业应用需求。 Vaadin...

oscar999的专栏 1万+

Vaadin组件代码示例

ListSelect // Create the selection component ListSelect select = new ListSelect("The List"); // Add some items (here by the item ID as the caption) select.addItems("Mercury", "Venus", "Earth", ...);

zj23qq的专栏 1041

SOComponents:一组来自各种Web组件的Vaadin Flow组件

SO组件 一组Vaadin流分量和抽象 依存关系 Ver 1.0.0或更高版本(Helper类)。 版本5.2.0或更高版本(用于字段格式化功能)。 如果您不希望使用此版本,请使用版本1.3.0以下的版本。 版本3.0.2或更高版本(用于TokensField)。 如果您不希望此依赖性,请使用版本1.7.0以下。 版本1.11(用于SVG图像处理)。 用例 有2种不同的方式使用此加载项。 正常使用 它可以用来利用此加载项中可用的组件(尤其是“字段”和“表单”)。 这里的“表单”概念支持一种机制,不需要直接使用Vaadin的“活页夹”类来创建数据输入表单。 一个隐藏的活页夹可以解决所有问题。 另外,还有两个值得注意的类:DataGrid和TreeDataGrid。 这些类分别是Vaadin的Grid和TreeGrid的增强版本。 值得一提的是两个布局类:ButtonLayout

vaadin23集成minio实现文件上传下载

【代码】vaadin23集成minio实现文件上传下载。

sl4379的博客 258

vaadin-select:类似于本地浏览器选择的可自定义Web组件。 Vaadin组件的一部分

<vaadin> | 是一个类似于本机浏览器select元素的Web组件,属于一部分。 < vaadin> </ vaadin> < script > document . querySelector ( 'vaadin-select' ) . renderer = root => { if ( root . firstElementChild ) { return ; } // Note that innerHTML is only used for demo purposes here!

vaadin教程

vaadin 开发的帮助文档。可供你很轻松的入门学习它。值得一看

Vaadin Web应用开发教程: 总结

到目前为止基本介绍了开发Vaadin使用的基本方法,包括概述,UI组件,布局方法,主题的使用,数据绑定及SQLContainer,可以满足初学者的需求,其它关于开发自定义UI组件,AJAX,使用插件,测试等后面在Vaadin高级进阶时在介绍。为方便查询,下面列出Vaadin Web应用开发教程相关文章。 Vaadin Web应用开发教程(1):概述 Vaadin Web应用开发教程(2):安装...

一个2年工作经验的程序测试员 86

Leraning Vaadin

Vaadin is a component-based Java web framework for making applications look great and perform well, making your users happy. Vaadin promises to make your user interfaces attractive and usable while easing your development efforts and boosting your productivity. After having read this book, you will be able to utilize the full range of development and deployment features offered by Vaadin while thoroughly understanding the concepts.

vaadin 上传下载案例

package com.rquest.riskmaster.component; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNo...

chenyu6160的博客 347

Vaadin7中文文档

非常实用的Vaadin7中文文档,里面详细介绍了vaadin的原理以及开发流程,和适合入门

vaadin官网demo

vaddin的例子,很棒的框架,做出来的非常好看而且容易学习,值得学习!

Yii Framework 开发教程(38) Zii组件-ProgressBar示例

CJuiProgressBar显示一进度条。它封装了JUI Progressbar插件。CJuiProgressBar显示一进度条。它封装了JUI Progressbar插件。 &lt;?php $this-&gt;widget('zii.widgets.jui.CJuiProgressBar', array( 'id'=&gt;'progress', 'value'=&gt;0, '...

mapdigit 176

OpenEarthMap 与 xBD 数据集合并

整合好的数据:链接: https://pan.baidu.com/s/1uJ_Zg6n-s3u1D49OW52D7Q?pwd=2vwx 提取码: 2vwx完整整合步骤与代码如下。

qq_60293814的博客 1599
上一篇: Android ApiDemos示例解析(126):Views->Layout Animation->1.Grid Fade
下一篇: Android ApiDemos示例解析(127):Views->Layout Animation->2. List Cascade
引路蜂
博客等级 码龄16年 3122粉丝 711原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值