Tuesday, November 24, 2009

Jsp File Upload Code. Insert image to Database Using blob

-------------------- In Jsp ---------------------------------------------

< FORM ENCTYPE="multipart/form-data" ACTION="../../StudentController?button=uploadimage" METHOD=POST>
< br>


< center>
< table>

< table width="409" border="0" cellspacing="1" cellpadding="5" bgcolor=#3399FF>
< tr>
< td>< strong>Choose photo to Upload:
< td>< input name="file" type="file" />
< /tr>
< tr>
< td> < /td>
< td>< INPUT TYPE="submit" VALUE="Upload File" >
< /tr>
< /table>
< /center>

< /FORM>


-------------------------------------------------------------------------------------

------------------------------ In Controller (Servlet) ------------------------------

out.println("in file upload");
// String txt_g_r_no = request.getParameter("txt_gr_no1");
// String cmb_school_name = request.getParameter("cmb_school_name1");
String gr_no = (String)session.getAttribute("gr_no");
String school_name = (String)session.getAttribute("school_name");
String sid = (String)session.getAttribute("sid");
_StudentBean.setTxt_g_r_no(gr_no);
_StudentBean.setCmb_school_name(school_name);
_StudentBean.setSid(sid);


try
{
int val =0;
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;

while (totalBytesRead < byteread =" in.read(dataBytes," file =" new" savefile =" file.substring(file.indexOf(" filename="\" savefile =" saveFile.substring(0," savefile =" saveFile.substring(saveFile.lastIndexOf(" lastindex =" contentType.lastIndexOf(" boundary =" contentType.substring(lastIndex" pos =" file.indexOf(" filename="\" pos =" file.indexOf(" pos =" file.indexOf(" pos =" file.indexOf(" boundarylocation =" file.indexOf(boundary," startpos =" ((file.substring(0," endpos =" ((file.substring(0,"> startPos)
{
saveFile="sid_"+sid+".jpg";
String path = application.getRealPath("\\View\\Student_Images\\");
String saveFile1 = path+"\\"+saveFile;
FileOutputStream fileOut = new FileOutputStream(saveFile1);
fileOut.write(dataBytes, startPos, (endPos - startPos));
_StudentBean.setFile_path(saveFile1);
_StudentModel.InsertImage(_StudentBean);

}

else
out.println("Error in uploading File");


}


}
catch(Exception e)
{
e.printStackTrace();
}
------------------------------------------------------------------------------------

----------------------------------- In Model -------
--------------------------------

--> In Model Create function which will be called from Controller

public void InsertImage(StudentBean _StudentBean)throws SQLException,IOException
{
System.out.println("in upload function file path :" + _StudentBean.getFile_path());
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/school";
ResultSet rs = null;
PreparedStatement psmnt = null;
FileInputStream fis;
try {

//_StudentBean.setTxt_g_r_no("1");
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");

File image = new File(_StudentBean.getFile_path());


psmnt = connection.prepareStatement("update student_master set image=? where school_name='"+_StudentBean.getCmb_school_name()+"' and g_r_no="+_StudentBean.getTxt_g_r_no());

fis = new FileInputStream(image);
psmnt.setBinaryStream(1, (InputStream)fis, (int)(image.length()));

int s = psmnt.executeUpdate();
if(s>0) {
System.out.println("Uploaded successfully !");
}
else {
System.out.println("unsucessfull to upload image.");
}
}
catch (Exception ex)
{
System.out.println("Found some error : "+ex);
}

finally {
connection.close();
psmnt.close();
}



}
-------------------------------------------------------------------------------------