Where is the p:fileUpload uploaded file saved and how do I change it?

前端 未结 2 536
隐瞒了意图╮
隐瞒了意图╮ 2020-12-15 08:31

I use the simple File upload of Primefaces in development with Netbeans. My test example is similar to to the Primefaces manual.
My question: where does the file

2条回答
  •  無奈伤痛
    2020-12-15 09:14

    I used simple mode, GlassFish 4.0 and PrimeFaces 4.0

    Backing Bean

    private UploadedFile file;
    private String destination="C:\\temp\\";
    
    public void upload() {
    
        System.out.println("uploading");
        if(file != null) {  
            System.out.println("the file is" +file);
            FacesMessage msg = new FacesMessage("Succesful" + file.getFileName() + " is uploaded.");  
            FacesContext.getCurrentInstance().addMessage(null, msg);  
    
            try {
               copyFile(file.getFileName(), file.getInputstream());
           }
           catch (IOException e) {
              e.printStackTrace();
           }
        }
        System.out.println("uploaf finished");
    }  
    
    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());
        }
    }
    

    JSF page

     
    

提交回复
热议问题