Image upload with Spring and Uikit

Asked

Viewed 254 times

0

Dear implementing upload in my project, I was guided by the project of Algaworks Brewer ( upload the photo of beer), using a front-end framework Uikit
The upload is being done, so I could notice that in a folder with the name "null" is serving as the place where the files are being stored, but there is something very strange occurring and I can’t say why.

Well first the image of the uploaded file is not appearing and I realize the 404 error is occurring in a GET, as I noticed that a folder is created inside the project folder only with the name null, when it should be the photo folder

I understand that it is not very revealing what I described more if someone who makes use of these technologies can comment

The project makes use of Spingboot, Thymeleaf and Bootstrapinserir a descrição da imagem aqui

1 answer

0

I also went through this problem, when the 404 is spring is not finding the folder path to save the photo, check if really the app created the folder inside the projeto project/.null/nomePasta. I advise you to do different instead of creating the folder within the project you can create it outside of the project, because, you will probably compile the project and run on Tomcat, when compiled it generates a project.war where are the compiled files from the app.

Here’s an example I used in my app to create the photos folder outside the app:

@Profile("local")
@Component
public class LogoPath {

private static final Logger logger = LoggerFactory.getLogger(LogoPath.class);

private Path local;

public static Path getDefaultPath() {
    //Servidor linux
    Path pathDefault = Paths.get("/app/fotos");
    if(Files.notExists(pathDefault, LinkOption.NOFOLLOW_LINKS)) {
        //No usuario do windows
        String userHome = System.getProperty("user.home");
        pathDefault = Paths.get(userHome, "fotos");
    }
    return pathDefault;
}

public LogoPath() {
    this(getDefaultPath());
}

public LogoPath(Path path) {
    this.local = path;
    criarPastas();
}

private void criarPastas() {
    try {
        Files.createDirectories(this.local);

        if(logger.isDebugEnabled()) {
            logger.debug("Pasta criada para salvar arquivos.");
            logger.debug("Pasta default: " + this.local.toAbsolutePath());
        }
    } catch (IOException e) {
        throw new RuntimeException("Erro criando pasta para salvar arquivos.", e);
    }

}

}

Browser other questions tagged

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