1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
Blob myblob=null;
String name=null;
ResultSet resultat=null;
Statement statement=null;
try{
//lien vers la base de données
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testjava","root",null);
statement= connection.createStatement();
//préparation de l'instruction SQL
String sql = "SELECT * FROM fichier WHERE id_doc ="+id+"";
resultat = statement.executeQuery(sql);
//récupération de l'image (BLOB)
while(resultat.next()) {
myblob = resultat.getBlob("content_doc");
name = resultat.getString("nom_doc");
}
}
catch(Exception e){
e.printStackTrace();}
BufferedInputStream bis =null;
BufferedOutputStream bos = null;
try{InputStream streamDuBlob = myblob.getBinaryStream();
bis = new BufferedInputStream(streamDuBlob);
bos = new BufferedOutputStream(res.getOutputStream());
}
catch(Exception e){
e.printStackTrace();}
byte[] input = new byte[1024];
boolean eof = false;
while (!eof) {
int length = bis.read(input);
if (length == -1) {eof = true;}
else {bos.write(input, 0, length);}
}
bos.flush(); |