Hello,
I have created below program and program is throwing FileNotFound exception when I pressed the submit button. The problem is coming due to JSP page is not able to find complete path of image. I have debug the JSP program and found that HTML form pass only image name without path that's why problem is coming. Can anyone resolve this problem.

##################  SQL Query ######################################

    CREATE TABLE IMAGEMAIN(ID INTEGER,IMAGE BLOB) ;

##################  HTML  Form ######################

     <form name="frm" method="post" action="index.jsp">
     <input type="text" name="hint">
     <input type="file" name="user_file">
     <input type="submit">

################### JSP PAGE ########################

try
{ 
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    System.out.println("Connection loaded");
    Connection con = DriverManager.getConnection("jdbc:odbc:project","image","image");
    System.out.println("Connection created");
    String ll=request.getParameter("user_file");
    String lo=request.getParameter("hint");
    File imgfile = new File(ll);

    FileInputStream fin = new FileInputStream(imgfile);

    PreparedStatement pre = con.prepareStatement("insert into IMAGEMAIN (id,image) values(?,?)");
    pre.setString(1,lo);
    pre.setBinaryStream(2,fin,(int)imgfile.length());
    pre.executeUpdate();
    pre.close();
}

catch(Exception E)
{
    out.println("the eror is  "+ E);
}

Due to security reasons, the request.getParameter will not work in the newer browser, I tried such as example, where the image used to be uploaded onto the server in the form of blob, I share that code with you but its in PHP, and here it is..

<?php
$fileUpload_name=($_FILES['file']['name']);
$fileUpload_size=($_FILES['file']['size']);
$fileUpload_type=($_FILES['file']['type']);
$fileUpload=( $_FILES['file']['tmp_name']);
$strDesc=$_POST["strDesc"];
if(empty($strDesc) || $fileUpload == "none")
die("You must enter both a description and file");
$dbServer = "localhost";
$dbDatabase = "myfiles";
$dbUser = "project";
$dbPass = "hacker";
$sConn = mysql_connect($dbServer, $dbUser, $dbPass) or die("Couldn't connect to database server");
$dConn = mysql_select_db($dbDatabase, $sConn) or die("Couldn't connect to database $dbDatabase");
$fileHandle = fopen($fileUpload, "r");
$fileContent = fread($fileHandle, $fileUpload_size);
$fileContent = addslashes($fileContent);
$dbQuery = "INSERT INTO myBlobs VALUES ";
$dbQuery .= "(0, '$strDesc', '$fileContent', '$fileUpload_type')";
mysql_query($dbQuery) or die("Couldn't add file to database");
echo "<h1>File Uploaded</h1>";
echo "The details of the uploaded file are shown below:<br><br>";
echo "<b>File name:</b> $fileUpload_name<br>";
echo "<b>File type:</b> $fileUpload_type <br>";
echo "<b>File size:</b> $fileUpload_size <br>";
echo "<b>Uploaded to:</b> $fileUpload <br><br>";
echo "<a href='index.html'>Add Another File</a>";
echo "</br>";
?>

<input type="file" name="file" size="20">
The task is same, here also we are fetching the file from an HTML form and loading it to the database..Hope will be helpful, if you want to do in JSP iteslf, there are some others methods, if there are any problems in that then share it here :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.