0

I am trying to figure out if it is possible to insert a file into the project using code. I have a jsp. page which has a filechooser for the user to choose a file from, and the file is then saved into a database table as a byte[]. I also save the name of the file (in form thisIsAFile.png) into the same table.

What I want to know is if it is possible to insert the file into the project resources?

I have created a file into the project where I would like to save the file into using code. I am aware that right clicking the project and doing an import is a way to add a file into the designated file manually, but I do not want that, instead I need a way for the file (in this case images only) to be saved into the project automatically, every time a user chooses a file to be saved into the database from the .jsp page. The path I am trying to insert the files into is gotten from a .properties file as /images/banners/{0} Then I only need a way to get the name (which contains the actual name + . + mime type of the file) from the database into the {0} and save it in the path as the byte[] from the corresponding row of the database. Any help is much appreciated.

Beryllium
  • 12,808
  • 10
  • 56
  • 86
ghoulfolk
  • 326
  • 7
  • 17
  • I am not sure, if I have understood your question. But the approach you have described in the 1st paragraph is fine. I cannot imagine a reason for doing it another way, because this approach separates data and project, and data is available cluster-wide. – Beryllium Sep 02 '13 at 07:25
  • so you are saying I should not insert the file into the project, just use the data directly from the database? The reason I am trying to put the data into the project is because the images are in the form of binary data in the database, and they take a lot of room. If I could somehow get them from the database into the project file I have designated them to, I could null the binary data in the database of the row that I moved, saving room and making backup and other procedures quicker. – ghoulfolk Sep 02 '13 at 07:32
  • If you are going to delete the DB contents then why don't you save the file initially directly to the filesystem? – Scary Wombat Sep 02 '13 at 07:36
  • there are also columns stating the locale, description, mimetype, width , length, dates of visibility, creator and the date of creation in the table, which I will still need to keep to reference on and to determine where and when the file will be shown. The only data I plan on maybe deleting (setting to null) from the db is the binary data column. But if the file can be saved directly to the filesystem, I am interested in knowing how this can be done (not manually using import) – ghoulfolk Sep 02 '13 at 07:41
  • It always needs space on a disk. In case of a database, it's consistent (and so is the backup). Otherwise you will have problems getting a consistent backup (which comprises both database *and* the files). If this is not important, you can find some ideas in [this answer and its comments](http://stackoverflow.com/a/18096976/2390083), but you can just omit the images from the database backup as well. – Beryllium Sep 02 '13 at 07:43
  • thank you for your attention to this question, I will review the ideas provided. – ghoulfolk Sep 02 '13 at 08:02

1 Answers1

0

Save the file directly to the filesystem when it is received by the servlet. You can save other data about the file to DB but instead of saving the file to the DB create a FileOutStream and save it to a File

see http://www.mkyong.com/java/how-to-convert-array-of-bytes-into-file/

After than you can set the img src tag to the physical location of the file, or to a servlet which will return the response as a outputstream

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64