commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "栗磊" <the3o...@gmail.com>
Subject Re: How save a file into DB
Date Tue, 22 Jan 2008 11:26:54 GMT
just don't create the File but use outputStream write into db as bytes


2008/1/22, Matteo Di Palma <dipalma.matteo@gmail.com>:
>
> Yes. I have a database for store images.
> With my code the files (images) are copy to DB, but there's a copy of
> image
> also into webapps/MyWebApp/ folder...don't like me this :(
>
> 2008/1/22, 栗磊 <the3oooo@gmail.com>:
> >
> > i don't actully get it. you mean the local database?
> >
> > 2008/1/22, Matteo Di Palma <dipalma.matteo@gmail.com>:
> > >
> > > Hi,
> > > I have created a file upload. Work, but I save the file in the first
> > time
> > > in
> > > a temporary folder and after into DB...but I want save the file only
> in
> > my
> > > DB.
> > > Do you have any suggestion?
> > >
> > > This is my code:
> > >
> > > ********************************************************
> > > DiskFileUpload dfu = new DiskFileUpload();
> > >
> > > try {
> > >     List fileItems = dfu.parseRequest(req);
> > >     Iterator it = fileItems.iterator ();
> > >     while (it.hasNext()) {
> > >          FileItem f = (FileItem) it.next();
> > >          File file=new File(getServletContext().getRealPath("/"),
> > > f.getName
> > > ());
> > >          f.write(file);
> > >          out.println ("File " + f.getName() + " saved in "
> > >                         + file.getAbsolutePath() + "<BR>");
> > >
> > >         Class.forName("com.mysql.jdbc.Driver");
> > >         Connection conn = DriverManager.getConnection
> > > ("jdbc:mysql://localhost/test",
> > >                                     "root", "root");
> > >         PreparedStatement ps = conn.prepareStatement("insert into
> > immagini
> > > values(null, ?)");
> > >         FileInputStream fis = new FileInputStream(file);
> > >         ps.setBinaryStream(1, fis, (int)file.length());
> > >         ps.executeUpdate();
> > >         ps.close();
> > >         conn.close();
> > >    }
> > > } catch (Exception e) { ...}
> > > ********************************************************
> > >
> >
> >
> >
> > --
> > Lilei
> > Software Development Group, Computer Center
> > BYD Company Limited
> > No.3001, Hengping Road, Pingshan, Longgang, Shenzhen, 518118, P.R.China
> > Tel: +86-755-89888888
> > Fax: +86-755-84202222
> >
>



-- 
Lilei
Software Development Group, Computer Center
BYD Company Limited
No.3001, Hengping Road, Pingshan, Longgang, Shenzhen, 518118, P.R.China
Tel: +86-755-89888888
Fax: +86-755-84202222
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message