> >
> > How can I store an *.gif or *.jpg file in a Database
> > and after display in a browser with servlet.
> > Specialy, I d'ont now how to read the picture-file
> > with a servlet and store this information in the
> > attribut of database. After, I want read the image
> > with a sevlet in the database and show in the browser.
> > Have somebody examples of servletcode??
> >
> > Very thanks for help
> >
A good idea is definately to get the oreilly util package
(www.servlets.com). It handels the multipart request from a HTTP post
operation. Here's an example:
try{
MultipartRequest m = new MultipartRequest(req, "/tmp");
Enumeration files = m.getFileNames();
String nome = m.getParameter("nome");
PrintWriter out = null;
try {
out = res.getWriter();
} catch (IOException ex){
ex.printStackTrace(System.out);
}
int InsertResult;
try{
Properties info = new Properties();
info.put("user", "user");
info.put("password", "password");
DbConnection db = new DbConnection(dbUrl, info);
Connection con = (Connection)db.getConnection();
Statement s = (Statement)con.createStatement();
InsertResult = (int)s.executeUpdate(Query);
if (InsertResult > 0){
Query = "select LAST_INSERT_ID() from marcas";
ResultSet r = (ResultSet)s.executeQuery(Query);
if (r.next()){
int id = r.getInt(1);
Query = "insert into marca_img values('" + id + "', ?, ?,
'', '')";
PreparedStatement ps =
(PreparedStatement)con.prepareStatement(Query);
try{
if (files.hasMoreElements()){
String name = (String)files.nextElement();
String filename = m.getFilesystemName(name);
String type = m.getContentType(name);
File f = m.getFile(name);
FileInputStream img = new FileInputStream(f);
ps.setString(2, type);
ps.setBinaryStream(1, img, (int)f.length());
int result = ps.executeUpdate();
img.close();
f.delete();
if (result < 1){
Then to show the image on a page, create a tag on the page <img
src="GetImage">
and the GetImage servlet should look something like this:
try{
dbconnection = new DbConnection(dbUrl, info);
connection = (Connection)dbconnection.getConnection();
Statement smt = (Statement)connection.createStatement();
String Query = "select image, content from marca_img where ndx="
+ id;
ResultSet rs = (ResultSet)smt.executeQuery(Query);
while (rs.next()){
response.setContentType(rs.getString(2));
try{
int BytesRead;
byte[] buf = new byte[64000];
InputStream dbin = rs.getBinaryStream(1);
while (((BytesRead=dbin.read(buf)) != -1)){
out.write(buf,0,BytesRead);
out.write(BytesRead);
}
out.flush();
rs.close();
smt.close();
} catch (IOException e) {
e.printStackTrace(System.out);
}
}
P.S. the ByteArray os declared as byte[64000] since I use mysql, and the
mysql jdbc driver has a bug, and cannot handle files of over 64k
--
Sven E. van 't Veer, llm.
Departamento de Desenvolvimento
Brasil Informatica.
http://www.brvip.com.br
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html