- 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."));
- }
- }
upload文件上传,保存为文件
最新推荐文章于 2024-04-17 10:15:32 发布