How to save the path of an image in the Database?

Asked

Viewed 3,756 times

5

I’m using JDBC and picking up an image to save in the database. However I want to save the image in the hard drive and save in the database only the way (path) hers.

I wanted to know which is the best kind I use to manipulate and save the image (Image or File). Since most of the functions of File are related to String;

The image is selected by form of html and passed as if it were a servlet, however, I use it in a Controller to make it a simpler and more cohesive code.

  • If you are going to insert only the path, why not a String ? File.getAbsolutePath() ?

  • The image is selected by the html form. As if it were a Servlet

2 answers

3

Is not possible for security reasons.

Quite simply: No website needs to know anything about the user’s file system. When a file is sent to a server only data is important.

In Firefox you can still view the mozFullPath that shows the Absolute path of a file using Javascript, but if you try to take/use this value it will return an empty string.

/* CTRL+SHIFT+J para ver a mensagem no console. */

document.getElementById('file').onchange = function(){
    console.dir(this.files[0]);
    console.log("mozFullPath: " + this.files[0]['mozFullPath']); // vai retornar ""
};
<input id='file' type='file'/>

You’ll see something like this:

mozfullpath

  • Got it. But I want to first save the file on my hard drive. To then pick up the Path, and so saves the path in the database. My biggest problem is to know what kind of variable I use to manipulate the image. Because if I use the Image I can’t save it in the system, and if I use the type File I can’t place the image inside the File type.

2


Saving the whole way

To save in a database you must save the way as a String.

Obviously your code that reads and writes the records can encapsulate this String into a File for example, recovering the String bank and creating a File or recovering the file path as String and recording in the database.

In that situation, do what is most convenient.

Organizing the archives

However, unless it is an absolutely necessary requirement of your system, I would not save the entire file path.

In general, it would be more appropriate to set a directory in the program configuration and save all selected files in that directory.

Then you can save only the names of the images in the database and, when you need to save the file, just do something like:

new File(configuracao.getPastaArquivos(), nomeArquivo)

Besides, the file name isn’t even necessary. If you have, for example, a table in the database where you save the file information, you can simply save the file in the folder using the ID as its name.

So the path on the disk would be something like this:

new File(configuracao.getPastaArquivos(), arquivo.getId())

This way, the file data (like your name) stays in the database and you have everything organized in a folder by ID.

It’s easier to back up, move files to another location if needed, and also avoids problems with special characters in names, spaces, and accents. Also allows situations where two system users send a file with the same name.

Browser other questions tagged

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