Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Having problem in reading the contents of a file uploaded
Hello,
Im having problem in reading the contents of a file uploaded. Can you help?
Also, Im using the button cancel to cancel the uploading. There are two uploading fields. How can I differentiate between the two fields when Im clicking on it ? One is named FileUpload and the other ParentFileUpload. I want it to be ike this -> If i click in the fFileUpload field, some function will be processed and if i click on ParentFileUpload field, some other functions occur. Can you help? Urgent.Below are the codes:
public class GenomeUI extends UI implements Upload.ProgressListener,Upload.FailedListener,Upload.SucceededListener, Upload.Receiver, Upload.FinishedListener{
protected Upload FileUpload, ParentFileUpload;
protected File tempFile,file;
protected Button btnOk,cancelProcessing;
protected Panel views,options;
protected String directory;
protected Long contentLength;
protected Label ProjectName;
protected HorizontalLayout processinglayout, afterprocessing;
protected Label l;
protected boolean cancelled=false;
protected VerticalLayout v;
@SuppressWarnings("deprecation")
protected ProgressBar progress;
protected AbsoluteLayout layout;
protected OptionGroup multiple;
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = GenomeUI.class)
public static class Servlet extends VaadinServlet {
}
@SuppressWarnings("deprecation")
@Override
protected void init(VaadinRequest request) {
layout = new AbsoluteLayout();
layout.setSizeFull();
setContent(layout);
ProjectName = new Label("Genome Rearrangement Problems");
layout.addComponent(ProjectName,"left:40%;right:0%;top:0%;bottom:0%");
//*************Setting Panel**************//
options = new Panel("Settings");
options.setSizeFull();
v = new VerticalLayout();
v.setSpacing(true);
//**************Uploading genome file***********//
FileUpload = new Upload("Upload genome file: ",this);
FileUpload.setReceiver(this);;
FileUpload.addListener((Upload.SucceededListener)this);
FileUpload.addListener((Upload.FailedListener)this);
FileUpload.addListener((Upload.ProgressListener)this);
FileUpload.addListener((Upload.FinishedListener) this);
//************Uploading parent file*************//
ParentFileUpload = new Upload("Upload parent file: ",this);
ParentFileUpload.setReceiver(this);
ParentFileUpload.addListener((Upload.SucceededListener)this);
ParentFileUpload.addListener((Upload.FailedListener)this);
ParentFileUpload.addListener((Upload.ProgressListener)this);
ParentFileUpload.addListener((Upload.FinishedListener) this);
//************* layout where processing will occur************//
processinglayout = new HorizontalLayout();
processinglayout.setSpacing(true);
processinglayout.setVisible(false);
//**************layout after processing occurs*************//
afterprocessing = new HorizontalLayout();
afterprocessing.setVisible(false);
//*************contains options for multiple algorithm*******//
multiple = new OptionGroup("Select algorithm/s");
multiple.setMultiSelect(true);
multiple.addItems("Insertion","Deletion","Transposition","Block Interchange");
//**************On clicking OK button process will start on panel views***************//
//Add -> if not all fields are filled
btnOk = new Button("Ok");
btnOk.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
// TODO Auto-generated method stub
Notification.show("Fill in properly");
}
});
//**********cancel the processing while uploading files*************//
//Add -> if button is clicked on file upload and parent file upload
cancelProcessing = new Button("Cancel");
cancelProcessing.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
// TODO Auto-generated method stub
cancelled=true;
FileUpload.interruptUpload();
ParentFileUpload.interruptUpload();
file.delete();
System.out.println("On pressing cancel file got deleted processing layout disappear");
processinglayout.setVisible(false);
}
});
cancelProcessing.setStyleName("small");
processinglayout.addComponent(cancelProcessing);
//***********Adding all components to the vertical layout*******//
v.addComponent(FileUpload);
v.addComponent(processinglayout);
v.addComponent(afterprocessing);
//Add-> parent parts not implemented processing layout and after processing
v.addComponent(ParentFileUpload);
v.addComponent(multiple);
v.addComponent(btnOk);
options.setContent(v);
layout.addComponent(options,"left:0%;right:0%;top:0%;bottom:0%");
//*********Progress Bar added to show processing*******//
progress =new ProgressBar();
progress.setWidth("100%");
processinglayout.addComponent(progress);
//****************View Panel************//
Panel views = new Panel("Views");
views.setSizeFull();
Label l2 = new Label("Design Mode");
views.setContent(l2);
views.setContent(l);
layout.addComponent(views,"left:40%;right:0%;top:7%;bottom:0%");
}
@Override
public void uploadSucceeded(SucceededEvent event) {
// TODO Auto-generated method stub
}
@Override
public OutputStream receiveUpload(String filename, String mimeType) {
FileOutputStream fos = null;
//FileUtils.ensureFolderExists(directory);
file = new File(directory, filename);
System.out.println(file.getName()); //checking filename
try {
fos = new FileOutputStream(file);
} catch (final java.io.FileNotFoundException e) {
throw new RuntimeException(e);
}
return fos; // Return the output stream to write to
}
@Override
public void updateProgress(long readBytes, long contentLength) {
this.contentLength=contentLength;
FileUpload.setVisible(false);
processinglayout.setVisible(true);
processinglayout.setCaption(file.getName());
progress.setEnabled(true);
progress.setValue(new Float(readBytes/(float) contentLength));
System.out.println("UPDATING PROGRESS");
}
@Override
public void uploadFinished(FinishedEvent event) {
processinglayout.setVisible(false);
afterprocessing.setVisible(true);
afterprocessing.setCaption(file.getName()+" uploaded successfully");
}
@Override
public void uploadFailed(FailedEvent event) {
try{
file.delete();
Notification.show("There was a problem uploading the file");
} catch (Exception e) {
// Silent exception. If we can't delete the file, it's not big problem. May the file did not even exist.
}
afterUploadFailed(event);
}
/** Override me to do something more than displaying the notification */
public void afterUploadFailed(FailedEvent event) {
}
public String getDirectory() {
return directory;
}
public File getFile() {
return file;
}
}