1

I've googled for some efficient image storage solutions and got very exciting line to read that you should store only image path rather that whole image in the database.

Now I am working on a Java web project based on MVC and is willing too know extra about this topic. Specifically I want to know if I am able to save my image directly to any image hosting website from my Servlet which instantly provide me a link that I will store in my database column for future use?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Asif
  • 4,980
  • 8
  • 38
  • 53

3 Answers3

3

you should store only image path rather that whole image in the database.

That's indeed recommended. Storing binary data in a database makes semantically no utter sense. You cannot index it, nor search in it, etcetera. It's just "dead" data. You can store it as good directly on the disk file system and then store its unique identifier (usually just the filename) in the database. The filename can be a varchar which is indexable (which thus allows for faster SELECT ... WHERE).


I want to know if I am able to save my image directly to any image hosting website from my Servlet which instantly provide me a link that I will store in my database column for future use?

I'm not sure what's your concrete problem here. You should realize that transferring bytes is after all just a matter of reading an arbitrary InputStream and writing it to an arbitratry OutputStream. Your concrete question should rather be, "How do I get an InputStream of the uploaded image?", or "How do I get an OutputStream to the local disk file system?", or maybe "How do I get an OutputStream to the image hosting website?".

Getting the uploaded image's InputStream is easy. All decent file upload APIs offer kind of a getInputStream() method. See also How to upload files to server using JSP/Servlet? Getting an OutputStream to a File on the local disk file system is also easy. Just construct a FileOutputStream around it.

File file = File.createTempFile(prefix, suffix, "/path/to/uploads");
InputStream input = uploadedFile.getInputStream();
OutputStream output = new FileOutputStream(file);
// Now write input to output.

String uniqueFileName = file.getName();
// Now store filename in DB.

Getting an OutputStream to some other host is a story apart. How do you want to connect to it? Using FTP? Use FTPClient#appendFileStream(). Or using HTTP (eek)? Use URLConnection#getOutputStream() or HttpClient. You should ask a more finer grained question about that if you stucks.

Finally, in order to get this image by URL (by either <img src> or direct request or whatever), read this answer: Reliable data serving.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Yeah i can learn and apply upload/download file on local file system..but my _concrete_ problem **was**: What if i want to upload the image directly to picasa or flickr etc. and gets the url of image in return to store in database.....here **was** means that it was a foolish thinking of mine :) storing on local file system is the right way.. Thanks – Asif Dec 09 '11 at 21:30
1

Here's a tutorial how to upload a file to a server using servlets/JSP. Then use Apache Commons IO to save it to some directory on the server. Save file's path in the database and use it next time this file is asked for.

Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
  • any other way? or a way that is described in an more easy way? actually i am new in java web development.. – Asif Dec 09 '11 at 13:04
  • 1
    Welcome to java web development. This is a pretty good exercise. I believe that many web frameworks offer such a functionality out of box. Which one do you use? – Boris Pavlović Dec 09 '11 at 13:47
-1

Sure, you can use apache commons api to upload your image to specified folder in server, and you can change its name as you wish, what ever it may be the image format you can save the path in Database and upload the image using servlet. apache commons api is for free, you will get the proper documentation apache site.

Jimshad Abdulla
  • 167
  • 2
  • 8