I have found examples for saving images to disk then to the database, but I can not find any examples to save to database without saving to disk.
Any example would be highly appreciated.
I have found examples for saving images to disk then to the database, but I can not find any examples to save to database without saving to disk.
Any example would be highly appreciated.
You need to read the URL and store it using a byte-array. after that you can create a ByteArrayInputStream from it and save it in the Database:
URL someUrl = null;
try{
someUrl = new URL("http://www.oracle.com/");
}catch(IOException ex){
//TODO: log error
}
if(someUrl != null){
byte[] arr = null;
try(InputStream is = someUrl.openStream()){
arr = readBytesFromStream(is);
}catch(IOException ex){
//TODO: log error
}
InputStream anotherStream = new BufferedInputStream(new ByteArrayInputStream(arr));
PreparedStatement stmt = createStatement();
stmt.setBinaryStream(index, anotherStream, arr.length);
stmt.execute();
}
I simplified it a bit by using the functions readBytesFromStream and createStatement, but i think you get the idea.