Inserting Image in Database with Java Web

Asked

Viewed 1,083 times

1

I’m having trouble inserting an image into a database. I already have a saving method and I’m including a photo field, but I’m not sure how to implement to upload the image.

Whoever can help me, I’ll be grateful.

Below are the codes:

package com.br.mvsistemasjavaweb.controler.usuarios;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.Part;

import org.apache.commons.io.IOUtils;

import com.br.mvsistemasjavaweb.controler.departamento.Departamentos;
import com.br.mvsistemasjavaweb.model.usuarios.TipoUsuarios;
import com.br.mvsistemasjavaweb.model.usuarios.Usuario;
import com.br.mvsistemasjavaweb.model.usuarios.UsuarioAtivo;
import com.br.mvsistemasjavaweb.repository.usuarios.Usuarios;
import com.br.mvsistemasjavaweb.service.usuarios.CadastroUsuariosService;
import com.br.mvsistemasjavaweb.util.FacesMessages;

@Named
@ViewScoped
public class UsuariosBean implements Serializable {

    private static final long serialVersionUID = 1L;

    @Inject
    private Usuarios usuarios;

    @Inject
    private CadastroUsuariosService cadastroUsuarios;

    @Inject
    private FacesMessages mensagens;

    private List<Usuario> todosUsuarios;

    private Usuario usuarioEdicao = new Usuario();

    private Usuario usuarioSelecionado;

    private Departamentos departamento;

    private Part foto;

    public void salvarUsuarios() throws IOException {
        // Linha para inserção da imagem
        this.usuarioEdicao.setFoto(IOUtils.toByteArray(this.foto.getInputStream()));
        this.cadastroUsuarios.salvar(usuarioEdicao);
        this.usuarioEdicao = new Usuario();
        this.consultar();
        mensagens.info("Usuario Salvo com Sucesso!");
    }

// ... Restante do código

View component that inserts the photo:

                <p:outputLabel value="Foto de Perfil" />
                <p:fileUpload value="#{usuariosBean.foto}" mode="simple" skinSimple="true"/> 
  • In general, it is not a good practice to store photos in a database, but as the problem is in the upload, could you inform how this declaring your form? And the version of Servlet is using?

  • The byte array that arrives on the server is correct or not?

  • I am developing JSF and Primefaces, there in Form I put the following statement: enctype="Multipart/form-data". Agora o erro é outro, ele esta apresentando: #{usuariosBean.salvarUsuarios}: java.lang.NullPointerException nesse EL que é chamado no metodo acima, a IDE mostra que o nullPointer é na seguinte linha: this.usuarioEdicao.setFoto(IOUtils.toByteArray(this.foto.getInputStream())); who can help me I thank.

1 answer

0


  • Friend Saito, is there any way I can send you the source code to see if you can help me??? I made the note but this presenting another error...

  • Yes, you can. If you could put that in a repository like https://bitbucket.org (allows private repositories) or https://github.com, it’s easier to collaborate. My email is: Emerson.Aito in gmail.

Browser other questions tagged

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