How to upload multiple images using spring boot

-1

I'm developing a manga project and want to save several photos in the database.

PagesController

@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

PagesRepository

@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 to the database?

Image of how the search for each photo is saved

    
asked by anonymous 11.12.2017 / 14:27

1 answer

0

The following example I took from this source:   link

@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 MultipartFile[] are multiple files uploaded.

Do not forget that you should use this form

<form name="uploadingForm" enctype="multipart/form-data" action="/" method="POST">  
    
11.12.2017 / 19:20