Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa HTML Adding image to database through jsp or HTML page ,while adding only image should show.
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


santhoshdk
Adding image to database through jsp or HTML page ,while adding only image should show.
4 Answer(s)      3 years and 9 months ago
Posted in : HTML


Adding image to database through jsp or HTML page,while adding only image should show. After that i need view that uploaded image from different jsp or HTML page.

please help me.

Thanks in Advance.


Advertisement
View Answers

April 6, 2011 at 5:00 PM


1)page.jsp:

<%@ page language="java" %>
<HTML>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>
<BODY> <FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<br><br><br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center>
<td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<tr>
<td colspan="2" align="center">&nbsp;</td>
</tr>
<tr>
<td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr>
<td colspan="2" align="center">&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Send File"> </td>
</tr>
<table>
</center>
</FORM>
</BODY>
</HTML>

2)upload.jsp:

<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.zip.*"%>
<%
String saveFile="";
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 < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><td><b>You have successfully upload the file:</b>
<% out.println(saveFile);%></td></tr></table>
<%
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/test";
ResultSet rs = null;
PreparedStatement psmnt = null;
FileInputStream fis;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
File f = new File(saveFile);
psmnt = connection.prepareStatement("insert into image(images) values(?)");
fis = new FileInputStream(f);
psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length()));
int s = psmnt.executeUpdate();
if(s>0){
System.out.println("Uploaded successfully !");
}
else{
System.out.println("Error!");
}
}
catch(Exception e){e.printStackTrace();}
}
%>
<a href="view.jsp">View Image</a>


April 6, 2011 at 5:01 PM


3)view.jsp:

<%@page import="java.sql.*,java.io.*"%>
<%
try{
    InputStream sImage;
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
      Statement st=con.createStatement();
      ResultSet rs=st.executeQuery("SELECT images FROM image");
      if(rs.last()) {
      byte[] bytearray = new byte[1048576];
      int size=0;
      sImage = rs.getBinaryStream(1);
      response.reset();
      response.setContentType("image/jpeg");
      while((size=sImage.read(bytearray))!= -1 ){
      response.getOutputStream().write(bytearray,0,size);
   }
}
 con.close();
}     
catch(Exception ex){
out.println("error :"+ex);
}
%>


August 2, 2011 at 7:35 PM


what is the data type if images field? please specify the size



August 18, 2011 at 11:59 AM


should be the data type in data base and thanks to the one who posted this example its helping me a lot



Related Tutorials/Questions & Answers:
Adding image to database through jsp or HTML page ,while adding only image should show.
Adding image to database through jsp or HTML page ,while adding only image should show.   Adding image to database through jsp or HTML page,while adding only image should show. After that i need view that uploaded image from
Show image and text on same jsp page
Show image and text on same jsp page  Hi all, I have to display image and text on the same jsp page. The text and image are both retrived from mysql database. The image is shown correctly on seperate jsp page but when shown
Advertisements
adding background image - Java Beginners
adding background image  how do i add background image to this code: import javax.swing.*; import java.awt.*; import java.awt.event.... stand"+text7.getText()+"in cm while you're weight is"+text8.getText()+"in lbs
Adding Image to Header and footer at generating Itext PDF
Adding Image to Header and footer at generating Itext PDF  i want to add Image to Header , am generating the PDF letter am able to add the text...("This is page: ", new Font(bf_courier)), true); header.setAlignment(Element.ALIGN_CENTER
Upload Image to Database through Servlet - JSP-Servlet
Upload Image to Database through Servlet  Hello, I make a application from where I upload the Image from local disk then store in DB.Before storing..., the server error may only be temporary. Trying the page again will often
Save profile and image to mysql database, and view the image in another jsp page
Save profile and image to mysql database, and view the image in another jsp page  Pls. need help in saving the profile info with the image in mysql database.. some basic code pls in jsp... thanks in advance
Retrieve image from mysql database through jsp
Retrieve image from mysql database through jsp... to retrieve image from mysql database through jsp code. First create a database.... mysql> create database mahendra; Note : In the jsp code given below, image
how to retrieve image from mysql database using java and show it in HTML img tag ?
how to retrieve image from mysql database using java and show it in HTML img tag ?  how to retrieve image from mysql database using java and show it in HTML img tag
Show spinner while loading image using JavaScript
Show spinner while loading image using JavaScript  Hello sir I am working on a web page which has .png image in middle of it. Other part of the page contains the set of links which , when clicked, the entire page reloads
jsp pages for dispatchaction for adding user to database
jsp pages for dispatchaction for adding user to database  ...-logic.tld" prefix="logic" %> <%@page contentType="text/html" pageEncoding="UTF-8...; <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <
how to display image and text in single jsp page from the mysql database
how to display image and text in single jsp page from the mysql database  hello please help me to display the image and text in single jsp page from mysql database if have any reference code please send me Thanks in advance
Image name,image path into database and image into folder using jsp
Image name,image path into database and image into folder using jsp  How to insert image path and image name into oracle database and image into folder using jsp
creating columns and adding label and textfield in the columns in a jsp page
creating columns and adding label and textfield in the columns in a jsp page  I have create a jsp page with 3 columns.....each column has a title,3 label and corresponding 3 text fields....can u send me the code
how to display one image on jsp through java
how to display one image on jsp through java  Hi, I wanted to display one image on my jsp file like social networking sites. But the scenario...://upload/image.jpg . what is the procedure to get that and display on my jsp page
how to upload an image from a jsp page to a mysql database table using jsp
how to upload an image from a jsp page to a mysql database table using jsp  how to upload an image from a jsp page to a mysql database table using jspstrong text
how to write a query for adding records in database
how to write a query for adding records in database  How write fire query in JSP for adding records in database
Insert Image in DB through Servlet - JSP-Servlet
Insert Image in DB through Servlet  Dear Sir, You write me: copy... A new Page displayed, check there a Round Shaped Image Called "download" click... Ohterwise send ur mailid Thanks' Rajanikant But Sir, Its show a error
how to show image as a link which path coming from database
how to show image as a link which path coming from database   iam not getting proper answer for it. I am using netbeans .the url coming instring from database ,want to display as image on jsp . please help me
Insert Image in DB through Servlet - JSP-Servlet
Insert Image in DB through Servlet  Dear Sir, My previous Query... image in Database using servlet to visit.... http://www.roseindia.net...(); System.out.println("Successfully display image from database ....."); ResultSet rs
UIView Image Background
UIView Image Background In this tutorial, we are going to show you how to add image at background in UIView. There are two different ways of adding background image in your iphone application.. 1. Either you can set the UIView
Insert Image in DB through Servlet - JSP-Servlet
Insert Image in DB through Servlet  Dear Sir, I am not able to solve my problem cocerning Inserting the image in Database through Servlet. I am usng... image from database! SQLException occured: java.sql.SQLException: ORA-00932
Insert Image In DB through Servlet - JSP-Servlet
Insert Image In DB through Servlet  Dear Sir, My previous Query... image in Database using servlet to visit.... http://www.roseindia.net... the image through servlet.But Sir, I am using weblogic so I do not want the reference
Insert Image In DB through Servlet - JSP-Servlet
Insert Image In DB through Servlet  Dear Sir, My previous Query that: I want to add the Image In databse using servlet. I have a program Its fine..., To insert image in Database using servlet to visit.... http://www.roseindia.net
Insert Image In DB through Servlet - JSP-Servlet
Insert Image In DB through Servlet  Dear Sir, I want to add the Image In databse using servlet. I have a program Its fine run on command prompt...()); } } } Whether Any entry will be made in web.xml when Insert Image in Databse using servlet
image in database
image in database  how to set image in database and retrieve it using servlet(java)in msaccess
Fetching image from database
Fetching image from database  I have uploaded image path and image name in database so, now how can i display that image using JSP or HTML page... in database is:E:\1003\54175\20110407121554 image name i have stored
Insert image in database
using . and then download the image and show on next jsp pg. No use of scriplets and jsp expression. only struts tags...Insert image in database  Want a jsp-struts program in which
adding data to the database with the use of jtable - Java Beginners
adding data to the database with the use of jtable  how can i add data to the database with the use of jtable. and also can able to view the records in the database in the table.. tnx :G
adding data to the database with the use of jtable - Java Beginners
adding data to the database with the use of jtable  how can i add data to the database with the use of jtable. and also can able to view the records in the database in the table.. tnx :G
Jsp Image prob - JSP-Servlet
Jsp Image prob   I m working on a project i which i have to upload a image in jsp to database and then on next page i will show that image from database so what should i do as i have done first phase
Unable to store the image into database
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... ###################################### CREATE TABLE IMAGEMAIN(ID INTEGER,IMAGE BLOB) ; ################## HTML Form
 

 

 

DMCA.com