srinureddyetta
asked on
file upload using servlet
i want to upload the file using sevlet and it should be stored in the mysql database,
please give code or links on this
please give code or links on this
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
u can use the fileupload for uploading file, http://commons.apache.org/fileupload/using.html
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
factory.setRepository(new File("C:/UploadFiles"));
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum size before a FileUploadException will be thrown
upload.setSizeMax(5000000);
List fileItems = upload.parseRequest(request);
// assume we know there are two files. The first file is a small
// text file, the second is unknown and is written to a file on
// the server
Iterator i = fileItems.iterator();
Hashtable table = new Hashtable () ;
while ( i.hasNext () )
{
FileItem fi = (FileItem)i.next();
// filename on the client
if ( !fi.isFormField () )
{
String fileName = fi.getName();
// save comment and filename to database
String fieldName = fi.getFieldName () ;
// write the file
File file = new File( "C:/UploadFiles/" + fileName.substring ( fileName.lastIndexOf ( "\\" ) ) ) ;
fi.write( file ) ;
table.put ( fieldName , file ) ;
}
}