Upload and Donwload JSF + Primefaces + Mysql files

Asked

Viewed 531 times

2

How to recover the file name saved as BLOB from the database and display on screen? I am using a loop, to recover, the value saved in the database column, but returns memory values ([B@2248bd40):inserir a descrição da imagem aqui

Another inconsistency that is occurring is: When the user performs the Upload to the database, the file is saved successfully (any file), but to run 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. It has some way to run the donwload of the file in original format, which has been saved?

Follows the method:

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);
    }
} 
  • Do you need to store the files in the database? Can’t you save them in some Torage or something? In the database just save the file path. I personally find it much easier.

1 answer

1

It is not possible to recover the name of a BLOB file by Java, it would be necessary to convert this BLOB to some other type of object for this, which would not be very nice to do.

In this case, what I suggest you do is:

  • Save the file name in another column of the database; or
  • Change the file storage way to recover it in some simpler way without using BLOB.

That’s it, following your current approach I do not imagine that it is easy to realize this functionality, maybe changing the approach everything becomes easier. Good luck!

Browser other questions tagged

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