鍍金池/ 教程/ Java/ UI 組件-Upload 組件
UI 組件-自定義組件
UI 布局-Panel
UI 組件-Slider 組件
UI 組件-Button
UI 組件-PasswordField
UI 布局-TabSheet 布局
Vaadin Web 應用的基本組成部分
UI 組件-Label
UI 組件-Link
UI 布局-GridLayout 布局
安裝開發(fā)環(huán)境
UI 組件-Tree 組件
UI組件-Select 組件
UI 布局-概述
UI 組件-RichTextArea
UI 組件-Table 組件
使用 Item 介面管理一組 Property
使用資源
UI 組件-TextArea
SQLContainer-編輯
SQLContainer-過濾及排序
UI 組件-TextField
UI 布局-HorizontalSplitPanel 和 VerticalSplitPanel 布局
SQLContainer-引用其它 SQLContainer
UI組件-ProgressIndicator組件
開始編寫 Web 應用
UI組件-Form組件
UI 布局-Accordion 布局
SQLContainer-使用 FreeformQuery
SQLContainer 概述
使用主題-創(chuàng)建和應用新主題
概述
UI 布局-AbsoluteLayout 布局
UI 組件-Upload 組件
使用主題-概述
UI 布局-FormLayout 布局
MenuBar 組件
UI 布局-VerticalLayout 和 HorizontalLayout 布局
UI 組件-Embedded 組件
UI 組件概述
使用 Container 介面管理一組 Item
UI 組件-LoginForm 組件
數(shù)據(jù)綁定-Property 接口
Vaadin 應用程序框架介紹
開始使用 SQLContainer
UI 組件-Checkbox
可視化界面編輯插件
數(shù)據(jù)綁定-概述

UI 組件-Upload 組件

Upload 組件用于向服務器上傳文件。它顯示一個文件名輸入框,一個文件選擇按鈕和一個上傳確認按鈕。

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

http://wiki.jikexueyuan.com/project/vaadin-web-development-tutorial/images/57.png" alt="" />

可以通過 setButtonCaption 修改”Upload” 按鈕的文字。對于”Browser” 按鈕由于瀏覽器安全方面的考慮,難以修改它的外觀,”Brower” 顯示語言取決于瀏覽器本身。 因此如果你想保持”Upload” 語言顯示的一致,你必須使用和“Browser”一樣的語言。 通常情況上傳的文件可以存放在文件系統(tǒng),數(shù)據(jù)庫或是臨時存放在內存中,Upload 組件將上傳的文件數(shù)據(jù)寫到一個 java.io.OutputStream 對象中,因此你可以使用你喜歡的方法來處理上傳到服務器的文件。 使用 Upload 組件,需要實現(xiàn) Upload.Receiver 接口來處理文件數(shù)據(jù),它將在用戶點擊”upload”按鈕后調用。 當文件上傳結束后,成功與否 Upload 組件將觸發(fā) Upload.FinishedEvent 事件??梢酝ㄟ^Upload.FinishedListener 接口來處理這個事件,事件參數(shù)包括文件名,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."));
    }
}

http://wiki.jikexueyuan.com/project/vaadin-web-development-tutorial/images/58.png" alt="" />

Tags: Java EE, Vaadin, Web

上一篇:UI 組件-Button下一篇:概述