can somebody send me the servlet code for the same?
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35220
7
posted
0
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.
/**
* @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 {
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);}
}
}
SCJP 5.0(84%), SCWCD 5.0(97%), SCDJWS 5.0(98%)
chinmaya mishra
Greenhorn
Joined: Jun 09, 2007
Posts: 7
posted
0
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
Marshal
Joined: Mar 22, 2005
Posts: 35220
7
posted
0
Googling for "Apache Commons FileUpload" will help you find that jar file fast.
chinmaya mishra
Greenhorn
Joined: Jun 09, 2007
Posts: 7
posted
0
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.