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

jerry VaadinWeb 2015年11月25日 收藏

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

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


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

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.OutputStream;
  4. import com.vaadin.terminal.FileResource;
  5. import com.vaadin.ui.*;
  6.  
  7. public class MyUploader extends CustomComponent
  8. implements Upload.SucceededListener,
  9. Upload.FailedListener,
  10. Upload.Receiver {
  11.  
  12. Panel root; // Root element for contained components.
  13. Panel imagePanel; // Panel that contains the uploaded image.
  14. File file; // File to write to.
  15.  
  16. MyUploader() {
  17. root = new Panel("My Upload Component");
  18. setCompositionRoot(root);
  19.  
  20. // Create the Upload component.
  21. final Upload upload =
  22. new Upload("Upload the file here", this);
  23.  
  24. // Use a custom button caption instead of plain "Upload".
  25. upload.setButtonCaption("Upload Now");
  26.  
  27. // Listen for events regarding the success of upload.
  28. upload.addListener((Upload.SucceededListener) this);
  29. upload.addListener((Upload.FailedListener) this);
  30.  
  31. root.addComponent(upload);
  32. root.addComponent(new Label("Click 'Browse' to "+
  33. "select a file and then click 'Upload'."));
  34.  
  35. // Create a panel for displaying the uploaded image.
  36. imagePanel = new Panel("Uploaded image");
  37. imagePanel.addComponent(
  38. new Label("No image uploaded yet"));
  39. root.addComponent(imagePanel);
  40. }
  41.  
  42. // Callback method to begin receiving the upload.
  43. public OutputStream receiveUpload(String filename,
  44. String MIMEType) {
  45. FileOutputStream fos = null; // Output stream to write to
  46. file = new File("/tmp/uploads/" + filename);
  47. try {
  48. // Open the file for writing.
  49. fos = new FileOutputStream(file);
  50. } catch (final java.io.FileNotFoundException e) {
  51. // Error while opening the file. Not reported here.
  52. e.printStackTrace();
  53. return null;
  54. }
  55.  
  56. return fos; // Return the output stream to write to
  57. }
  58.  
  59. // This is called if the upload is finished.
  60. public void uploadSucceeded(Upload.SucceededEvent event) {
  61. // Log the upload on screen.
  62. root.addComponent(new Label("File " + event.getFilename()
  63. + " of type '" + event.getMIMEType()
  64. + "' uploaded."));
  65. // Display the uploaded file in the image panel.
  66. final FileResource imageResource =
  67. new FileResource(file, getApplication());
  68. imagePanel.removeAllComponents();
  69. imagePanel.addComponent(new Embedded("", imageResource));
  70. }
  71.  
  72. // This is called if the upload fails.
  73. public void uploadFailed(Upload.FailedEvent event) {
  74. // Log the failure on screen.
  75. root.addComponent(new Label("Uploading "
  76. + event.getFilename() + " of type '"
  77. + event.getMIMEType() + "' failed."));
  78. }
  79. }