Vaadin使用PipedInputStream和PipedOutputStream示例上传

时间:2019-02-22 17:03:43

标签: vaadin vaadin8

我刚刚开始学习Vaadin 8,我的第一个示例是“上传”按钮。我陷入了一个问题,就是我无法连续几个小时解决问题。

在这里,

我正在用receiveUpload方法返回PipedOutputStream,

这是receiveUpload方法的代码,

  public OutputStream receiveUpload(String filename, String mimeType) {
  this.fileName = filename;
  this.mimeType = mimeType;
  try {

     pipedOutputStream = new PipedOutputStream();
     pipedInputStream = new PipedInputStream(pipedOutputStream);

    if (filename == null || filename.trim().length() == 0) {
        upload.interruptUpload();
     } else {
     }
  } catch (Exception e) {
     e.printStackTrace();
  }
  return pipedOutputStream;
}

在uploadSucceeded方法中,我需要使用pipedinputstream并将其发送给另一个方法以将该流加载到数据库中

  public void uploadSucceeded(SucceededEvent event) {
     try { 
        fileUploadOperation.upload(pipedInputStream); --> I need to push all the stream data in one go into a method to generate a file at the business layer
     } catch (Exception e) {
        e.printStackTrace();
     }
  }

当我运行该应用程序时,它挂了很长时间,无法确定它在哪里。后来我注意到,管道输入流和管道输出流都应该在单独的线程中创建,或者至少其中之一在单独的线程中创建,但不知道如何处理。

任何帮助

我要粘贴完整的课程以获取更多信息,

public class WebCommunityView implements Receiver, FailedListener, SucceededListener, StartedListener, FinishedListener {

       private PipedOutputStream pipedOutputStream = null;
       private PipedInputStream pipedInputStream = null;
       private Upload upload = null;
       private String fileName = null, mimeType = null;
       private Grid<FileListProperties> fileListGrid = null;

       public final static WebCommunityView newInstance(WebContentScreen screen) {
          vw.initBody();
          return vw;
       }

       protected void initBody() {

          VerticalLayout verticalLayout = new VerticalLayout();

          fileListGrid = new Grid<FileListProperties>();
          fileListGrid.addColumn(FileListProperties::getCreatedDate).setCaption("Date");
          fileListGrid.addColumn(FileListProperties::getFileName).setCaption("File Name");
          fileListGrid.addColumn(FileListProperties::getUserName).setCaption("User Name");
          fileListGrid.addComponentColumn(this::buildDownloadButton).setCaption("Download");

          fileListGrid.setItems(loadGridWithFileInfo());

          upload = new Upload("", this);
          upload.setImmediateMode(false);
          upload.addFailedListener((Upload.FailedListener) this);
          upload.addSucceededListener((Upload.SucceededListener) this);
          upload.addStartedListener((Upload.StartedListener) this);
          upload.addFinishedListener((Upload.FinishedListener) this);

          Label fileUploadLabel = new Label("Label"));

          verticalLayout.addComponent(currentListLabel);
          verticalLayout.addComponent(fileListGrid);
          verticalLayout.addComponent(fileUploadLabel);
          verticalLayout.addComponent(upload);
          mainbody.addComponent(verticalLayout);
       }

       @Override
       public void uploadSucceeded(SucceededEvent event) {

             try {
                //Model Layer
                fileUploadOperation.upload(pipedInputStream);
                fileUploadOperation.commit();

             } catch (Exception e) {
                e.printStackTrace();
             }

       }

       @Override
       public void uploadFailed(FailedEvent event) {
          if (event.getFilename() == null) {
             Notification.show("Upload failed", Notification.Type.HUMANIZED_MESSAGE);
          }

             try {
                            //Model Layer
                fileUploadOperation.abort();
             } catch (Exception e) {
                e.printStackTrace();
             }
       }


       public OutputStream receiveUpload(String filename, String mimeType) {
          this.fileName = filename;
          this.mimeType = mimeType;
          try {

             pipedOutputStream = new PipedOutputStream();

             new Thread() {
                public void run() {
                   try {
                      System.out.println("pipedInputStream Thread started");
                      pipedInputStream = new PipedInputStream(pipedOutputStream);

                   } catch (Exception e) {
                      e.printStackTrace();
                   }
                };
             }.start();

             if (filename == null || filename.trim().length() == 0) {
                screen.displayMessage("Please select a file to upload !", WebContentScreen.MESSAGE_TYPE_WARNING);
                upload.interruptUpload();
             } else {
                Properties properties = new Properties();
                properties.setProperty("NAME", fileName);
                properties.setProperty("MIME_TYPE", mimeType);
                //Model Layer
                fileUploadOperation.initialize(properties);

             }
          } catch (Exception e) {
             e.printStackTrace();
          }
          System.out.println("pipedOutputStream:"+pipedOutputStream);
          return pipedOutputStream;
       }

       private List<FileListProperties> loadGridWithFileInfo() {
          List<FileListProperties> list = null;
          DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");

          try {
             list = new ArrayList<FileListProperties>(1);
             Collection<FileInfo> fileInfoList = fileCommandQuery.lstFilesForDownload();
             for (Iterator iterator = fileInfoList.iterator(); iterator.hasNext();) {

                FileInfo fileInfo = (FileInfo) iterator.next();
                Properties properties = fileInfo.getProperties();

                Collection<String> mandatoryParameters = fileInfo.getMandatoryProperties();

                FileListProperties fileListProperties = new FileListProperties();

                for (Iterator iterator2 = mandatoryParameters.iterator(); iterator2.hasNext();) {
                   String key = (String) iterator2.next();
                   String value = properties.getProperty(key);
                   if (key != null && key.equalsIgnoreCase("NAME")) {
                      fileListProperties.setFileName(value);
                   } else if (key != null && key.equalsIgnoreCase("USER_NAME")) {
                      fileListProperties.setUserName(value);
                   } else if (key != null && key.equalsIgnoreCase("CREATED_DATE")) {
                      Calendar calendar = Calendar.getInstance();
                      calendar.setTimeInMillis(1550566760000L);
                      fileListProperties.setCreatedDate(dateFormat.format(calendar.getTime()));
                   }
                }
                if (fileListProperties != null) {
                   list.add(fileListProperties);
                }
             }

          } catch (Exception e) {
             e.printStackTrace();
          } finally {
             dateFormat = null;
          }
          return list;
       }

       private Button buildDownloadButton(FileListProperties fileListProperties) {
          Button button = new Button("...");
          button.addClickListener(e -> downloadFile(fileListProperties));
          return button;
       }

       private void downloadFile(FileListProperties fileListProperties) {

       }
    }

1 个答案:

答案 0 :(得分:1)

示例中的问题是您需要在单独的线程中进行实际的文件处理。 Viritin add-on包含一个名为 UploadFileHandler 的组件,该组件大大简化了这种用法。它将为您提供InputStream来使用。该组件的The integration test包含这种用法示例。

此外,我关于该主题的recent blog entry可能会有所帮助。