[RESOLVIDO] Upload em banco de dados com primefaces

0 respostas
E

Boa tarde, Pessoal estou postando parte de um código que consegui fazer executar upload direto no banco de dados se uso de arquivo temporario

Eu procurei um tempão e não consegui encontrar algo parecido então para quem sofreu que nem eu ou esta sofrendo lá vai

public void novoArquivo(){
        Integer ordem = daoArquivo.ordem(noticia.getiNoticia());
        noticiaArquivo = new NoticiaArquivo();
        noticiaArquivo.setNoticiaArquivoPK(new NoticiaArquivoPK(noticia.getiNoticia(), ordem));
    }
    
    public void upload(FileUploadEvent event) throws IOException {
        novoArquivo();

        UploadedFile arquivo = event.getFile();
        InputStream in = new BufferedInputStream(arquivo.getInputstream());
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        
        try{
            for (int readNum; (readNum = in.read(buf)) != -1;) {
                bos.write(buf, 0, readNum);
            }
        } catch (IOException ex) {
            ex.getMessage();
        }
        
        byte[] bytes = bos.toByteArray();
        
        noticiaArquivo.setNome(arquivo.getFileName());
        noticiaArquivo.setTipo(arquivo.getContentType());
        noticiaArquivo.setArquivo(bytes);
        
        daoArquivo.gravar(noticiaArquivo);
        
    }
Criado 9 de agosto de 2012
Respostas 0
Participantes 1