I have two classes one contains the fileupload and save file method... I want from the MB to call this class, which is the most correct way

Asked

Viewed 45 times

1

follows the fileupload class

public class FileUpload{

private String destination="D:\\tmp\\";

public void upload(FileUploadEvent event) {  
    FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");  
    FacesContext.getCurrentInstance().addMessage(null, msg);
    // Do what you want with the file        
    try {
        copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
    } catch (IOException e) {
        e.printStackTrace();
    }

}  

public void copyFile(String fileName, InputStream in) {
       try {


            // write the inputStream to a FileOutputStream
            OutputStream out = new FileOutputStream(new File(destination + fileName));

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }

            in.close();
            out.flush();
            out.close();

            System.out.println("New file created!");
            } catch (IOException e) {
            System.out.println(e.getMessage());
            }
}

I want to call this method doUpload of my ManagedBean, it is in a separate package because it will be reused several times

@managedBean
public class MBminhaTela{

chamar objeto doUpload da classe FileUpload

}
  • Have you checked the showcase how to do?

  • the method is direct in managedBean what I want is to call this method a useful package

  • Okay, this method will be static?

  • would be the best way to call him

  • If I understand, right you want to call the method a static class within the mbMinhaTela class correct? If it is, just call the method...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.