upload文件上传,保存为文件

  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. public class MyUploader extends CustomComponent  
  7.                         implements Upload.SucceededListener,  
  8.                                    Upload.FailedListener,  
  9.                                    Upload.Receiver {  
  10.   
  11.     Panel root;         // Root element for contained components.  
  12.     Panel imagePanel;   // Panel that contains the uploaded image.  
  13.     File  file;         // File to write to.  
  14.   
  15.     MyUploader() {  
  16.         root = new Panel("My Upload Component");  
  17.         setCompositionRoot(root);  
  18.   
  19.         // Create the Upload component.  
  20.         final Upload upload =  
  21.                 new Upload("Upload the file here"this);  
  22.   
  23.         // Use a custom button caption instead of plain "Upload".  
  24.         upload.setButtonCaption("Upload Now");  
  25.   
  26.         // Listen for events regarding the success of upload.  
  27.         upload.addListener((Upload.SucceededListener) this);  
  28.         upload.addListener((Upload.FailedListener) this);  
  29.   
  30.         root.addComponent(upload);  
  31.         root.addComponent(new Label("Click 'Browse' to "+  
  32.                 "select a file and then click 'Upload'."));  
  33.   
  34.         // Create a panel for displaying the uploaded image.  
  35.         imagePanel = new Panel("Uploaded image");  
  36.         imagePanel.addComponent(  
  37.                          new Label("No image uploaded yet"));  
  38.         root.addComponent(imagePanel);  
  39.     }  
  40.   
  41.     // Callback method to begin receiving the upload.  
  42.     public OutputStream receiveUpload(String filename,  
  43.                                       String MIMEType) {  
  44.         FileOutputStream fos = null// Output stream to write to  
  45.         file = new File("/tmp/uploads/" + filename);  
  46.         try {  
  47.             // Open the file for writing.  
  48.             fos = new FileOutputStream(file);  
  49.         } catch (final java.io.FileNotFoundException e) {  
  50.             // Error while opening the file. Not reported here.  
  51.             e.printStackTrace();  
  52.             return null;  
  53.         }  
  54.   
  55.         return fos; // Return the output stream to write to  
  56.     }  
  57.   
  58.     // This is called if the upload is finished.  
  59.     public void uploadSucceeded(Upload.SucceededEvent event) {  
  60.         // Log the upload on screen.  
  61.         root.addComponent(new Label("File " + event.getFilename()  
  62.                 + " of type '" + event.getMIMEType()  
  63.                 + "' uploaded."));  
  64.           
  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. }  

猜你喜欢

转载自oylx.iteye.com/blog/2322267