How to create a Thread in Spring Boot?

0

I'm starting a file upload file application, where it writes the path in the database and the image on the server, when carrying out a test I can send the image to the folder, now I need to be able to create the temporary location when I submit the form with other properties the image can be submitted at the same moment, but for this to happen you need this temporary location with Thread.

Get the template uploaded via from this repository , this application is only sending the file to folder, but at first I only need it at the moment.

My application is currently like this;

@RestController
@RequestMapping("/files")
public class FotosResource {

//  @PostMapping
//  public void upload(@RequestParam MultipartFile files) {
//      disco.salvarFoto(files);
//  }   

    @PostMapping
    public DeferredResult<String> upload(@RequestParam MultipartFile files) {
        DeferredResult<String> resultado = new DeferredResult<>();

        Thread thread = new Thread(new FotoStorageRunnable(files, resultado));
        thread.start();



        return resultado;
    }


}

And I tried to create this Thread;

public class FotoStorageRunnable implements Runnable {

    @Autowired
    private Disco disco;

    private MultipartFile files;
    private DeferredResult<String> resultado;

    public FotoStorageRunnable(MultipartFile files, DeferredResult<String> resultado) {
        this.files = files;
        this.resultado = resultado;
    }

    @Override
    public void run() {
        disco.salvarFoto(files);
        resultado.setResult("OK! Foto recebida!");

    }

}

When running the application in STS generated this error in the console, I am using Spring Boot;

Exception in thread "Thread-9" java.lang.NullPointerException
    at com.algaworks.contato.storage.FotoStorageRunnable.run(FotoStorageRunnable.java:22)
    at java.lang.Thread.run(Unknown Source)

You are indicating that the error is in this line of code;

  

disk.saveFoto (files);

How do I get the error fixed?

The expected behavior would be for it to work the same way it was behaving before the change, being able to send the selected file to folder.

============= UPDATE =========

@Component
public class Disco {

    @Value("${contato.disco.raiz}")
    private String raiz;

    @Value("${contato.disco.diretorio-fotos}")
    private String diretorioFotos;

    public void salvarFoto(MultipartFile files) {
        this.salvar(this.diretorioFotos, files);
    }

    public void salvar(String diretorio, MultipartFile arquivo) {
        Path diretorioPath = Paths.get(this.raiz, diretorio);
        Path arquivoPath = diretorioPath.resolve(arquivo.getOriginalFilename());

        try {
            Files.createDirectories(diretorioPath);
            arquivo.transferTo(arquivoPath.toFile());           
        } catch (IOException e) {
            throw new RuntimeException("Problemas na tentativa de salvar arquivo.", e);
        }       
    }
}
    
asked by anonymous 05.10.2018 / 12:34

0 answers