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
Your problem may have been solved, your upload is not working. You are loading the static file.
– Viktor Hugo
I’m working on that part yet
– slyfer