File Upload PrimeFaces - Save image in project context

0

Good evening I'm with a javaEE project jsf2.2 and primefaces, in which I use the component of primefaces so that the user sends an image and it is saved in a folder inside the project, type: "resources / images", so that through a html page another user accesses it. What I need is a java code that saves the image to disk and would not be temporary like in that codepackage com.almasystems.financeiro.controller.maz;

import java.io.File; import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; import java.util.List;

import javax.annotation.PostConstruct; import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import javax.faces.view.ViewScoped; import javax.inject.Named;

import org.primefaces.event.FileUploadEvent; import org.primefaces.model.UploadedFile;

import com.almasystems.finance.util.ArchiveUtil;

@Named @ViewScoped public class UploadNotaBean implements Serializable {

private static final long serialVersionUID = 1L;

Private List Files = new ArrayList

@PostConstruct
public void postConstruct() {
    arquivos = new ArrayList<>(ArquivoUtil.listar());
}

public void upload(FileUploadEvent event) {
    UploadedFile uploadedFile = event.getFile();

    try {
        File arquivo = ArquivoUtil.escrever(uploadedFile.getFileName(), uploadedFile.getContents());

        arquivos.add(arquivo);

        FacesContext.getCurrentInstance().addMessage(null, 
                new FacesMessage("Arquivo enviado", "O arquivo " + arquivo.getName() + " foi salvo!"));            
    } catch (IOException e) {
        FacesContext.getCurrentInstance().addMessage(null, 
                new FacesMessage(FacesMessage.SEVERITY_WARN, "Erro", e.getMessage()));
    }
}

public List<File> getArquivos() {
    return arquivos;
}

} que grava mas é temporária. esse codigo monta o caminho temporario:.. package com.almasystems.financeiro.util;

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Arrays; import java.util.List;

public class FileUtil {

public static File escrever(String name, byte[] contents) throws IOException {
    File file = new File(diretorioRaizParaArquivos(), name);

    OutputStream out = new FileOutputStream(file);
    out.write(contents);
    out.close();

    return file;
}

public static List<File> listar() {
    File dir = diretorioRaizParaArquivos();

    return Arrays.asList(dir.listFiles());
}

public static java.io.File diretorioRaizParaArquivos() {
    File dir = new File(diretorioRaiz(), "arquivos");

    if (!dir.exists()) {
        dir.mkdirs();
    }

    return dir;
}

public static File diretorioRaiz() {
    // Estamos utilizando um diretório dentro da pasta temporária. 
    // No seu projeto, imagino que queira mudar isso para algo como:
    // File dir = new File(System.getProperty("user.home"), "arquivo");
    File dir = new File(System.getProperty("user.temp"), "almasystems");

    if (!dir.exists()) {
        dir.mkdirs();
    }

    return dir;
}

} 'so I can not see the image on another page, I can only download and temporary. If friends have a solution, thank you.

    
asked by anonymous 22.01.2018 / 00:17

0 answers