How to upload multiple images using spring boot

Asked

Viewed 1,390 times

-1

I am developing a Mangas project and want to save several photos in the database.

Paginascontroller

@RequestMapping(value="/pagina", method = RequestMethod.POST) 
    public @ResponseBody ResponseEntity<PaginasEntity> cadastrarPaginas
    (@RequestParam(value="fotos") MultipartFile fotos) throws IOException {
        PaginasEntity pagina = new PaginasEntity();

        pagina.setFotos(fotos.getBytes());
        //No angular seleciono um arquivo e seto no PaginasEntity.
         pagRepository.save(pagina);
        return  ResponseEntity.ok().build();
    }

//Busca a pagina de cada capitulo, capitulo/1/pagina1

@RequestMapping(value="/capitulo2/{id}/{id2}", method = RequestMethod.GET)
    public HttpEntity<byte[]> procurarPorCapitulo2(@PathVariable(value="id") Long id, @PathVariable(value="id2") Long id2) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.IMAGE_JPEG);
        /*List<PaginasEntity> pagina = pagRepository.procurarFotosPorCapitulos(id);*/
        return new ResponseEntity<byte[]>(pagRepository.procurarFotosPorCapitulos(id, id2), httpHeaders, HttpStatus.OK);
    }

Paginasentity

@Entity
@Table(name="paginas")
public class PaginasEntity {

    private Long id;    
    private Long numeroPagina;
    private Blob pages; 
    private byte[] fotos;
    private  CapitulosEntity capitulo;

    @Column
    @Lob
    public byte[] getFotos() {
        return fotos;
    }

    public void setFotos(byte[] fotos) {
        this.fotos = fotos;
    }

 //Getter and Setter

Paginasrepository

@Repository
public interface PaginasRepository extends CrudRepository<PaginasEntity, Long> {

    @Query("SELECT p.fotos FROM PaginasEntity p WHERE p.capitulo.id =?1 and p.numeroPagina=?2")
    byte[] procurarFotosPorCapitulos(Long id, Long id2);
}

So far, save photo by photo, one at a time, how to select multiple photos and save in database?.

Image of how each photo is saved and searched

1 answer

0

The following example I took from this source: https://hellokoding.com/uploading-multiple-files-example-with-spring-boot/

@RequestMapping(value = "/fileuploud", method = RequestMethod.POST)
    public String uploadingPost(@RequestParam("uploadingFiles") MultipartFile[] uploadingFiles) throws IOException {
        for(MultipartFile uploadedFile : uploadingFiles) {
            File file = new File(uploadingdir + uploadedFile.getOriginalFilename());
            uploadedFile.transferTo(file);//faz a transferencia para a variável file
        }

        return "redirect:/";
    }

Where the MultipartFile[] are multiple files sent.

Don’t forget that you should use this form

<form name="uploadingForm" enctype="multipart/form-data" action="/" method="POST">  

Browser other questions tagged

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