Format the name of a file to save Java - Spring MVC / Struts2

1

I am looking for a way to save a file, although I have the logic to capture it, I am saving it with the name of the image, what I would like to do is capture the file and format the name for example

Date_file_name to be avoided if you upload a file that has the same step.

Currently I'm doing it in this way in struts2 I would also like to know how to do it in Spring-MVc:

introducir el código aquí
private File userImage;
    private String userImageContentType;
    private String userImageFileName;

    private HttpServletRequest servletRequest;

    @Override
    public String execute() throws Exception{
         try {
                String filePath = servletRequest.getSession().getServletContext().getRealPath("/images/productos");

             System.out.println("FilePatch: " + filePath);
             System.out.println("Genero: " + genero);
                File fileToCreate = new File(filePath, this.userImageFileName);
                FileUtils.copyFile(this.userImage, fileToCreate);
                nombreimagen = "images/productos/"+userImageFileName;
    
asked by Gonzalo Zago 28.07.2017 в 18:21
source

2 answers

1

You can use UUID to avoid duplicates

UUID uuid = UUID.randomUUID();

String filePath = ...
String extension = FilenameUtils.getExtension(this.userImageFileName); // de org.apache.commons.io.FilenameUtils

File file = new File(filePath + uuid.toString() + "." + extension);

As these data I regularly keep in the database, I have a field for the original name, for example if you upload foto1.jpg you can have in your base

Archivo
| ID | nombreOriginal | path
| 1  | foto.jpg       | 7483e87d-e249-4110-bc3c-0ee76396ce01.jpg

Or you can simply log in with the registry ID

Archivo
| ID | nombreOriginal 
| 1  | foto.jpg       

And have it in your file system

/ruta/a/archivos/1

For when you want to download the file you can check its original name and based on its extension you can assign the content type suitable

response.setContentType
    
answered by 28.07.2017 в 18:38
0

This can be useful for the format you want to achieve:

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
    static final SimpleDateFormat formateador = new SimpleDateFormat("yyyy-M-dd-HH-mm-ss-SS");

    public static void main(String[] args) {
        String nombreOriginal = "foto.jpg";
        String nombre, extension;

        int punto = nombreOriginal.lastIndexOf(".");
        if (punto > 0) {
            nombre = nombreOriginal.substring(0, punto);
            extension = "." + nombreOriginal.substring(punto + 1);
        } else {
            nombre = nombreOriginal;
            extension = "";
        }
        String fecha = formateador.format(new Date());
        String nombreDeArchivo = String.format("%s_%s%s", nombre, fecha, extension);
        System.out.println(nombreDeArchivo);
    }
}

You can adjust the date and time format to what you want to achieve. The "SS" at the end gives you the milliseconds and usually that's enough to avoid name collisions.

    
answered by 22.11.2017 в 22:24