1 Replies - 1530 Views - Last Post: 30 April 2011 - 03:02 PM

#1 natasha1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-March 11

uploading an image to a web page

Posted 11 March 2011 - 07:51 AM

I am trying out an application to upload image to database but I am getting an exception,

I have two jsp files , The 1st jsp ie img.jsp contains the form which passes request to update.jsp

img.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="content-language" content="en" />
    <meta name="robots" content="all,follow" />

    <meta name="author" lang="en" content="All: Your name [www.url.com]; e-mail: [email protected]" />
    <meta name="copyright" lang="en" content="Webdesign: Nuvio [www.nuvio.cz]; e-mail: [email protected]" />

    <meta name="description" content="..." />

    <meta name="keywords" content="..." />

    <link rel="stylesheet" media="screen,projection" type="text/css" href="css/reset.css" />
    <link rel="stylesheet" media="screen,projection" type="text/css" href="css/main.css" />
    <!--[if lte IE 6]><link rel="stylesheet" type="text/css" href="css/main-msie.css" /><![endif]-->
    <link rel="stylesheet" media="screen,projection" type="text/css" href="css/style.css" />
    <link rel="stylesheet" media="print" type="text/css" href="css/print.css" />

    <title>Social Networking for Social Responsibility</title>


</head>

<body>

<div id="main">

    <!-- Header -->
    <div id="header">

        <table>
        	<tr>
          		<td width="800" colspan="12" align="left">
		 		<img src="tmp/earth.gif" WIDTH="50" HEIGHT="50" BORDER="0" ALT="">
				
				<FONT SIZE="6" COLOR="#330099"><strong>Social Networking for Social Responsibility</strong></FONT>
				</td> 
			</tr>
			</table>

		<br>
        <hr class="noscreen" />

        
    </div> <!-- /header -->
    
    <!-- Tray -->
    <div id="tray">

        

    <hr class="noscreen" />
    </div> <!-- /tray -->

    <!-- Promo -->
    <div id="col-top"></div>
    <div id="col" class="box">
    
         <!-- /ribbon (design/ribbon.gif) -->
        
        <!-- Screenshot in browser (replace tmp/browser.gif) -->
        <a href="#"><img src="tmp/home.jpg" width="255" height="177" alt="" /></a> 
        
        <div id="col-text">

            <h2 id="aim"><span></span>Upload Proof</h2>
            <form name=f1 method=post action="./update.jsp">

            
            <table>
           
			<tr>
			<td>
            <font size="3" face="Georgia"><strong>Give the path of image to upload:</strong></font></td>
            <td> <input type="text" name="imgid"></input></td>
            </tr>
            
            
            
            </table>
            <br>
            
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
               <input type="submit" value="Upload" >
            
           

        </div> <!-- /col-text -->
    
    </div> <!-- /col -->
    <div id="col-bottom"></div>
    
    <hr class="noscreen" />
    
    <!-- 3 columns -->

    
    
        
        

         
        
     

    <!-- Footer -->
    
</div> <!-- /main -->

</body>





update.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">






<html>


   
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload proof</title>
</head>
    

<body>
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>




<%! Connection con;
    Statement stmt;
	String imgid1;
	int updateQuery=0;
	String queryString;
	ResultSet rs;
	PreparedStatement pstatement;
%>
<%
    
	 imgid1=request.getParameter("imgid");

	
   
		
	try
	{
		Class.forName("oracle.jdbc.OracleDriver");
		con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE ","system","manager");
		stmt = con.createStatement();
       
            
        
        queryString="INSERT INTO imgup1(imgno,ephoto) VALUES(?,?)";
		pstatement=con.prepareStatement(queryString);
		pstatement.setInt(1,101);

		File photo=new File(imgid1);
		FileInputStream photoStream= new FileInputStream(photo);
		pstatement.setBinaryStream(2,photoStream,(int)photo.length());
		
		
		
	updateQuery=pstatement.executeUpdate();
		if(updateQuery!=0)
			{
			
			
			%> 
				<script language="javascript">
				document.location.href="img.jsp";
				alert("Inserted");
				</script>
				
			<%}
		else {
			  %>
				<script language="javascript">
				document.location.href="img.jsp";
				alert("Inserte incomplete");
				</script>
		
	<%
        }
	}catch(Exception e)
	{System.out.println(e);
		  %>
			<script language="javascript">
			document.location.href="img.jsp";
			alert();
			</script>
	<%
	}
	finally
	{
		try
		{
			if(pstatement!=null)
			{
				pstatement.close();
			}
			if(con!=null)
			{
				con.close();
			}
		}
		catch(SQLException sqle)
		{
			System.out.println("Unable to close database connection");
		}
	}
%>
</body>

</html>




structure of table in oracle is

Name Null? Type
----------------------------------------- -------- ----------------------------

IMGNO NUMBER(4)
EPHOTO BLOB

I get an alert as "incorrect path" coz i have used in my code.

Is This A Good Question/Topic? 0
  • +

Replies To: uploading an image to a web page

#2 sas1ni69  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 431
  • Joined: 04-December 08

Re: uploading an image to a web page

Posted 30 April 2011 - 03:02 PM

Hi there,

Do you get the error before or after selecting the image?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1