• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

file uploading to db using servlet

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a web application which will uoload the file into data base(oracle).

i have html file to browse file as "upload.html"

<form action="x">
File: <input type="file" name="upFile">

<input type="submit" name="submit" value="upload">
</form>


my servlet code is:


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class InsertMessage extends HttpServlet{

public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{

response.setContentType("text/html");

PrintWriter out=response.getWriter();


//what Code to upload the file to database.

}
}




Can any body help me in this regard?

can somebody send me the servlet code for the same?




 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "upload to the database"? Do you wish to store the bytes that make up the file in a binary field in the database? If so, you can use an attribute of type BLOB.

Servlets can handle file uploads by way of the Apache Commons FileUpload library.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chinmaya mishra wrote:I have a web application which will uoload the file into data base(oracle).

i have html file to browse file as "upload.html"


Can any body help me in this regard?

can somebody send me the servlet code for the same?



hi,

you have to use multipart formdata and parse input, see servlet file upload
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First you have to write <from ENCTYPE='multipart/form-data' ...>
in your html form
Here is the full code

<html>
<form method="post" action="/uploadFile" enctype="multipart/form-data">
Name
<input type="text" name="uname"/>
File
<input type="file" name="upfile"/>
<input type="submit"/>
</form>

</html>



---------------------------My servlet---------------------------
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;


/**
* @author sm23772
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class UploadFile extends HttpServlet {

public void doPost(HttpServletRequest req,HttpServletResponse res)
{
try{

FileUpload fup=new FileUpload();
boolean isMultipart = FileUpload.isMultipartContent(req);
// Create a new file upload handler
System.out.println(isMultipart);
DiskFileUpload upload = new DiskFileUpload();

// Parse the request
List /* FileItem */ items = upload.parseRequest(req);

Iterator iter = items.iterator();
while (iter.hasNext()) {

FileItem item = (FileItem) iter.next();

if (item.isFormField()) {
System.out.println("its a field");
} else {
System.out.println("its a file");
System.out.println(item.getName());
File cfile=new File(item.getName());
File tosave=new File(getServletContext().getRealPath("/"),cfile.getName());
item.write(tosave);
}
}

}catch(Exception e){System.out.println(e);}
}
}
 
chinmaya mishra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply.

Do i need to add any jar file to my class path for the package "org.appache.commons.fileupload"

because when i compiled it i got error as that package/class can not be found.

if i need to add any jar file can you tell me that jar file name.

thanks
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Googling for "Apache Commons FileUpload" will help you find that jar file fast.
 
chinmaya mishra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:What do you mean by "upload to the database"? Do you wish to store the bytes that make up the file in a binary field in the database? If so, you can use an attribute of type BLOB.

Servlets can handle file uploads by way of the Apache Commons FileUpload library.





I want to store the txt file in database so that the same file i can retrieve and see the content. i will store file in db as BLOB. can you send me the servlet code.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's lots of example code at http://commons.apache.org/fileupload/using.html

If you're not proficient in JDBC, start reading at http://java.sun.com/docs/books/tutorial/jdbc/index.html
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic