How to retrieve the file name saved as BLOB from the database and display on the screen? I am using a loop to retrieve, the value saved in the database column, but it returns memory values ([B @ 2248bd40):
Another inconsistency, which is occurring is: When the user uploads to the database, the file is successfully saved (any file), however to execute the donwload I must inform the code, and only allows the output of the file in the extension formatted in the source code (Example: pdf, doc, etc). The problem is, it will only come out according to the defined extension. Is there any way to download the file in original format, which was saved?
The method follows:
public void download() {
ResultSet rs;
try {
Connection con = Conexao.getConnection();
PreparedStatement st = con.prepareStatement("SELECT image FROM uploadfile WHERE codigo = (?) ");
st.setInt(1, codigo);
rs = st.executeQuery();
while (rs.next()) {
InputStream stream = rs.getBinaryStream("image");
file = new DefaultStreamedContent(stream, "image/jpg", "Arquivo.pdf");
}
con.close();
FacesMessage message = new FacesMessage("Exito", " File descarregado com sucesso.");
FacesContext.getCurrentInstance().addMessage(null, message);
} catch (Exception erro) {
erro.printStackTrace();
FacesMessage message = new FacesMessage("Erro ao executar download.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}