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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* class permettant la personalisation de l'appli par insertion d'un logo enregistré en bdd<br />
* - Revoie un flux de byte<br />
* @param request la requête du client
*/
public class Img extends HttpServlet {
static final long serialVersionUID = 133L;
public void doGet(final HttpServletRequest request, final HttpServletResponse response)
throws IOException, ServletException {
Connection conn = null;
ResultSet rs = null;
Blob blob = null;
byte[] filebyte = null;
String file = "";
//récupération du fichier stocké en bdd
file = "images/logo.gif";
String sql = "SELECT LOGO FROM t_parametre LIMIT 1";
if(index.GestMat.timerInitialise)
{try{conn = gestionBdd.PoolDeConnexion.getConnection();
rs = conn.createStatement().executeQuery(sql);
if(rs.next())
{blob = rs.getBlob(1);
try{
filebyte = blob.getBytes(1,(int)blob.length());}
catch(final NullPointerException npe){filebyte = fileWebContent(file);}}
rs.close();
conn.close();
}catch(final SQLException e){}
finally{try{if(rs !=null){rs.close();rs=null;}}catch(final SQLException e){};
try{if(conn !=null){conn.close();conn=null;}}catch(final SQLException e){};}}
else
{filebyte = fileWebContent(file);}
if(filebyte==null){filebyte = fileWebContent(file);}
if (filebyte != null)
{response.reset();
response.setHeader("Cache-control", "Public, max-age=31536000, must-revalidate");
response.setHeader("Expires", index.GestMat.dteHeaderExpire);
response.setContentType("image/gif");
OutputStream os = response.getOutputStream();
os.write(filebyte);
os.flush();
os.close();}
}
/**
* Récupération d'un tableau de byte du fichier passé en arg
* @return
*/
public byte[] fileWebContent(String file){
String FileName = this.getServletContext().getRealPath(file);
byte[] fileWebContent = null;
try{
InputStream ips = new FileInputStream(FileName);
int taille = ips.available() ;
fileWebContent = new byte[taille];
ips.read(fileWebContent) ;
ips.close();
}catch(final Exception ioe){ioe.printStackTrace();}
return(fileWebContent);
}
} |
Partager