Problems transforming file into an array of bytes

Asked

Viewed 452 times

0

Good evening, I’m trying to get a pdf file from a xhtml, and when it comes to saving it in the bank, I want to turn it into an array of bytes, for this, in my DAO I do so:

public boolean CadastrarAluno(Aluno a) throws FileNotFoundException {
    Connection con = Conexao.getConnection();
    PreparedStatement stmt = null;
    try {
        String sql = "INSERT INTO aluno(arquivo) values(?)";
        stmt = con.prepareStatement(sql);

        File file = a.getArquivorecebido();

        FileInputStream fis = new FileInputStream(file);
        System.out.println(file.exists() + "!!");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        try {
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum); // no doubt here is 0
                // Writes len bytes from the specified byte array starting at offset off to this
                // byte array output stream.
                System.out.println("read " + readNum + " bytes,");
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        byte[] bytes = bos.toByteArray();

        stmt.setBytes(1, bytes);
        stmt.executeUpdate();
        System.out.println("Query-------" + stmt);
        return true;
    } catch (SQLException ex) {
        System.out.println(ex);
        return false;
    } finally {
        Conexao.closeConnection(con, stmt);
    }
}

Part of my xhtml that receives the file

<p:fileUpload id="declaracao" value="#{alunoMB.aluno.arquivorecebido}" mode="simple" dragDropSupport="false" update="messages" sizeLimit="100000" fileLimit="1"/>

My model class:

private int idAluno;
    private File arquivorecebido;

    public File getArquivorecebido() {
        return arquivorecebido;
    }

    public void setArquivorecebido(File arquivorecebido) {
        this.arquivorecebido = arquivorecebido;
    }

But when I run, returns me the error:

java.lang.NullPointerException
    at java.io.FileInputStream.<init>(FileInputStream.java:130)

It gives to understand that I have to initialize this file input stream, but I did not understand how, if someone can give a north I thank

1 answer

0


Resolved with :

        //converter arquivo

     File file = new File("C:\\Users\\user\\Downloads\\documento.pdf");
      byte[] bFile = new byte[(int) file.length()];
      FileInputStream fileInputStream = null;
      try {
       fileInputStream = new FileInputStream(file);
       fileInputStream.read(bFile);
       fileInputStream.close();
       System.out.println("Meu array--------"+bFile); // Olha o array de byte
      }catch(Exception e){
       System.out.println(e);
      }
  • Your problem may have been solved, your upload is not working. You are loading the static file.

  • I’m working on that part yet

Browser other questions tagged

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