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="" />