You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Matteo Di Palma <di...@gmail.com> on 2008/01/22 12:02:20 UTC

How save a file into DB

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) { ...}
********************************************************

Re: How save a file into DB

Posted by Matteo Di Palma <di...@gmail.com>.
Thanks :)
I have seen that DiskFileUpload is a deprecated class, so I have edit my
source...now work well :D
What do you thinks? In your opinion is correct?

............................................................................................................
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List /* FileItem */ items = upload.parseRequest(req);

Iterator iter = items.iterator();
while (iter.hasNext()) {
     Class.forName("com.mysql.jdbc.Driver");
     FileItem item = (FileItem) iter.next();

     Connection conn = DriverManager.getConnection("jdbc:mysql://localhost
/test","root", "root");
     PreparedStatement ps = conn.prepareStatement("insert into
immaginivalues(null, ?)");


     InputStream uploadedStream = item.getInputStream();
     ps.setBinaryStream(1, uploadedStream, (int)item.getSize());

     ps.executeUpdate();
     ps.close();
     conn.close();
}

Re: How save a file into DB

Posted by 栗磊 <th...@gmail.com>.
DiskFileUpload dfu = new DiskFileUpload();

try {
  List fileItems = dfu.parseRequest(req);
  Iterator it = fileItems.iterator();
  while (it.hasNext()) {
     FileItem f = (FileItem) it.next();
     //the code down here means create a file on the path you gave,if u
don't want,just delete it
     //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");
//but i am not sure about the code you write here
//does it work insert the bytes into you DB?
     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();
}


2008/1/22, Matteo Di Palma <di...@gmail.com>:
>
> Can you give me an example please?
> Can you tell me EXACTLY what lines should change the following code?
> There are days that I try uselessly : (
> 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();
> }
>
> ............................................................................................
> Very very thanks,
> Matteo
>



-- 
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

Re: How save a file into DB

Posted by Matteo Di Palma <di...@gmail.com>.
Can you give me an example please?
Can you tell me EXACTLY what lines should change the following code?
There are days that I try uselessly : (
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();
}
............................................................................................
Very very thanks,
Matteo

Re: How save a file into DB

Posted by 栗磊 <th...@gmail.com>.
just don't create the File but use outputStream write into db as bytes


2008/1/22, Matteo Di Palma <di...@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, 栗磊 <th...@gmail.com>:
> >
> > i don't actully get it. you mean the local database?
> >
> > 2008/1/22, Matteo Di Palma <di...@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

Re: How save a file into DB

Posted by Matteo Di Palma <di...@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, 栗磊 <th...@gmail.com>:
>
> i don't actully get it. you mean the local database?
>
> 2008/1/22, Matteo Di Palma <di...@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
>

Re: How save a file into DB

Posted by 栗磊 <th...@gmail.com>.
i don't actully get it. you mean the local database?

2008/1/22, Matteo Di Palma <di...@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