Load Multiple Files and Rename them before saving them

0

Good morning, my problem is as follows, I am loading multiple files from a web app, the flow is correct, only I have not managed to rename the files to a predefined name standard before saving them in the destination directory. .

public void guardarMultiplesArchivos(List<MultipartFile> files, String directorioRoot, BigDecimal subDirectorio) throws IOException {
    for (MultipartFile file : files) {
        guardarArchivo(file,directorioRoot, subDirectorio);
    }
}

private void guardarArchivo(MultipartFile file, String directorioRoot, BigDecimal subDirectorio) throws IOException {

    if (file.isEmpty()) {
        return;
    }


    if (!isValidoContentType(file.getContentType())) {
        return;
    }

    byte[] bytes = file.getBytes();
    Path path = Paths.get(directorioRoot+File.separator+subDirectorio+File.separator+file.getOriginalFilename());
    Files.write(path, bytes);
}

Thank you ...

    
asked by Alejandro H M 12.05.2018 в 18:28
source

1 answer

0

It's simple. In the MultipartFile object itself you have a method called "transferTo (File f)", so your code should look something like this:

File nuevoArchivo = new File(<ruta_donde_guardar><separador><nombre_archivo_segun_estandar>);
file.transferTo(nuevoArchivo);

If, for whatever reason, this does not work, you create the destination file in the same way, you get its outputstream and you write the bytes of the file received in it:

File nuevoArchivo = new File(<ruta_donde_guardar><separador><nombre_archivo_segun_estandar>);
FileOutputStream outStream = new FileOutputStream(nuevoArchivo);
outStream.write(file.getBytes());

I hope to be of help.

    
answered by 12.05.2018 / 19:28
source