Vaadin 使用PipedInputStream和;PipedOutputStream示例
我刚开始学习Vaadin 8,我的第一个例子是上传按钮。我被困在一个问题中,好几个小时都无法解决这个问题 在这里 我在receiveUpload方法中返回PipedOutputStreamVaadin 使用PipedInputStream和;PipedOutputStream示例,vaadin,vaadin8,Vaadin,Vaadin8,我刚开始学习Vaadin 8,我的第一个例子是上传按钮。我被困在一个问题中,好几个小时都无法解决这个问题 在这里 我在receiveUpload方法中返回PipedOutputStream public OutputStream receiveUpload(String filename, String mimeType) { this.fileName = filename; this.mimeType = mimeType; try { pipedOutputSt
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;
}
这是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;
}
在UploadSuccessed方法中,我需要获取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) {
}
}
公共类WebCommunityView实现Receiver、FailedListener、SucceedListener、StartedListener、FinishedListener{
私有PipedOutputStream PipedOutputStream=null;
私有PipedInputStream PipedInputStream=null;
私有上传=空;
私有字符串fileName=null,mimeType=null;
私有网格fileListGrid=null;
公共最终静态WebCommunityView新实例(WebContent屏幕){
vw.initBody();
返回大众;
}
受保护的void initBody(){
VerticalLayout VerticalLayout=新的VerticalLayout();
fileListGrid=新网格();
fileListGrid.addColumn(FileListProperties::getCreatedDate).setCaption(“日期”);
fileListGrid.addColumn(FileListProperties::getFileName).setCaption(“文件名”);
fileListGrid.addColumn(FileListProperties::getUserName).setCaption(“用户名”);
fileListGrid.addComponentColumn(this::buildDownloadButton).setCaption(“下载”);
setItems(loadGridWithFileInfo());
上传=新上传(“,此);
upload.setImmediateMode(false);
upload.addFailedListener((upload.FailedListener)this);
upload.addSucceededListener((upload.SucceededListener)this);
upload.addStartedListener((upload.StartedListener)this);
upload.addFinishedListener((upload.FinishedListener)this);
标签文件上传标签=新标签(“标签”);
verticalLayout.addComponent(currentListLabel);
addComponent(fileListGrid);
addComponent(fileUploadLabel);
verticalLayout.addComponent(上传);
mainbody.addComponent(垂直布局);
}
@凌驾
公共无效上载成功(SucceedeEvent事件){
试一试{
//模型层
上传(pipedInputStream);
fileUploadOperation.commit();
}捕获(例外e){
e、 printStackTrace();
}
}
@凌驾
公共无效上载失败(FailedEvent事件){
if(event.getFilename()==null){
Notification.show(“上传失败”,Notification.Type.HUMANIZED_消息);
}
试一试{
//模型层
fileUploadOperation.abort();
}捕获(例外e){
e、 printStackTrace();
}
}
public OutputStream receiveUpload(字符串文件名、字符串mimeType){
this.fileName=文件名;
this.mimeType=mimeType;
试一试{
pipedOutputStream=新的pipedOutputStream();
新线程(){
公开募捐{
试一试{
System.out.println(“pipedInputStream线程已启动”);
pipedInputStream=新的pipedInputStream(PipedOutStream);
}捕获(例外e){
e、 printStackTrace();
}
};
}.start();
如果(filename==null | | filename.trim().length()==0){
screen.displayMessage(“请选择要上载的文件!”,WebContentScreen.MESSAGE\u TYPE\u警告);
upload.interruptUpload();
}否则{
属性=新属性();
setProperty(“名称”,文件名);
setProperty(“MIME_类型”,mimeType);
//模型层
fileUploadOperation.initialize(属性);
}
}捕获(例外e){
e、 printStackTrace();
}
System.out.println(“pipedOutputStream:+pipedOutputStream”);
返回管道输出流;
}
私有列表loadGridWithFileInfo(){
List=null;
DateFormat DateFormat=新的SimpleDateFormat(“dd MMM yyyy”);
试一试{
列表=新阵列列表(1);
集合fileInfoList=fileCommandQuery.lstFilesForDownload();
for(Iterator Iterator=fileInfoList.Iterator();Iterator.hasNext();){
FileInfo FileInfo=(FileInfo)迭代器.next();
Properties=fileInfo.getProperties();
Collection mandatoryParameters=fileInfo.getMandatoryProperties();
FileListProperties FileListProperties=新的FileListProperties();
for(迭代器迭代器2=mandatoryParameters.Iterator();迭代器2.hasNext();){
字符串键=(字符串)迭代器2.next();
字符串值=properties.getProperty(键);
if(key!=null&&key.equalsIgnoreCase(“NAME”)){
fileListProperties.setFileName(值);
}else if(key!=null&&key.equalsIgnoreCase(“用户名”)){
fileListProperties.setUserName(值);
}else if(key!=null&&key.equalsIgnoreCase(“创建日期”)){
C