image upload and stored in database

image upload and stored in database

How can i upload a image and store that image in a database
View Answers

May 31, 2010 at 1:13 PM

Hi Friend,

Try the following code:

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.*" %>

<% 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);
String 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;

FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><td><b>You have successfully upload the file by the name of:</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 file(file_data) values(?)");
//psmnt.setString(1,saveFile);
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("unsucessfull to upload file.");
}
}
catch(Exception e){e.printStackTrace();}
}
%>

Thanks

December 26, 2011 at 1:07 PM

could you please give the database also.









Related Tutorials/Questions & Answers:
image upload and stored in database - JSP-Servlet
image upload and stored in database  How can i upload a image and store that image in a database
image upload and stored in database - JSP-Servlet
image upload and stored in database  How can i upload a image and store that image in a database
Advertisements
image upload and stored in database - JSP-Servlet
image upload and stored in database  How can i upload a image and store that image in a database  Hi Friend, Try the following code: 1)page.jsp: Display file upload form to the user UPLOAD
Resize image stored in database
Resize image stored in database  hi, Can any one please send code for how to resize image stored in database in blob datatype in jsp, i want to resize it to 140(w)*160(h) please help me
upload image to database
upload image to database  i am try to upload image to MySql database using netbeans. when jsp execute it return no error. but also data does inserted in database. i am using blob datatype and preopared statement
how to display or retrive an image in jsp whose path is stored in oracle database
how to display or retrive an image in jsp whose path is stored in oracle database  how to display or retrive an image in jsp whose path is stored in oracle database and the image is stored in my pictures folder
Image upload in mysql database using jsp servlet
Image upload in mysql database using jsp servlet  Hello, I need code to insert image in mysql database, I have seen the code which is already in your portal but it is not inserting image into database it save in the folder
how to store image in folder and stored image path in mysql database using JSP
how to store image in folder and stored image path in mysql database using JSP  how to store image in folder and stored image path in mysql database using JSP
insert name city and upload image in database using mysql and jsp
insert name city and upload image in database using mysql and jsp   insert name city and upload image in database using mysql and jsp
how to store image upload path to mssql database
how to store image upload path to mssql database  hi there!!, i need help in storing the image upload path into the database. basically i just use file select to upload the image from jsp to database. however when i click submit
how to store image upload path to mssql database
how to store image upload path to mssql database  hi there!!, i need help in storing the image upload path into the database. basically i just use file select to upload the image from jsp to database. however when i click submit
how to store image upload path to mssql database
how to store image upload path to mssql database  hi there!!, i need help in storing the image upload path into the database. basically i just use file select to upload the image from jsp to database. however when i click submit
how to store image upload path to mssql database
how to store image upload path to mssql database  hi there!!, i need help in storing the image upload path into the database. basically i just use file select to upload the image from jsp to database. however when i click submit
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 Image in DB I want to check this program. I include the commons
upload an image
upload an image  Hello, i would like to upload an image to the database. how can i do it? what field type should i set in the database? thanx
Fastest way to upload huge raw data in database - c#, Java or stored procedure
Fastest way to upload huge raw data in database - c#, Java or stored procedure  I have huge raw data that needs to uploaded in database. This procedure repeats every 15days. I need to know which method would be fastest to upload
image upload
be upload in the server and their path should be stored in database either...image upload  Hello sir I want to upload image or any other type... to database The given code allow the user to browse and upload selected file
image upload in java
image upload in java  Hi, I am working with java. In my application i want to give facility to user to add and change image. I use open dialog box... and that path stored into database, for furhter retrivation . Please guide me
Upload Image and save in database using jsp-servlet mvc
Upload Image and save in database using jsp-servlet mvc  Here is my...; <table> <tr><TD ><B>Upload Image</B></TD> <td><input type="file" name="Image" size="20" value="">
Upload Image and save in database using jsp-servlet mvc
Upload Image and save in database using jsp-servlet mvc  Here is my...; <table> <tr><TD ><B>Upload Image</B></TD> <td><input type="file" name="Image" size="20" value=""><
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
upload image
upload image  I want to upload image from user using Jdbc connectivity and servlets and Html pages. I tried code but getting error using multi parts,so please send the code
image stored in mysql
image stored in mysql   how does an image store in mysql using java. gime an exam. code. thanks..   Insert Image in MySQL
upload image
upload image  how can i retreive image from mysql using jsp in netbeans.The image type is varchar with size 200.the image is uploaded succesfully.The... is stored in mysql... 2.retreive.jsp //the image is retreived using the id
image upload
image upload  How to access image file from database using jsp?  ... rs=st.executeQuery("SELECT images FROM image WHERE id = 1"); if(rs.next...("image/jpeg"); while((size=sImage.read(bytearray))!= -1
upload image data
upload image data  Hii Upload image dat in csv files
upload image data
upload image data  Hii Upload image dat in csv files
Upload image
Upload image  Hai i beginner of Java ME i want code to capture QR Code image and send to the server and display value in Mobile Screen i want code in Java ME .java extension.. Regards senthil   To capture an image
image upload in webapp/upload folder
image upload in webapp/upload folder  sir i want to store upload image in my project directory WebApp/Upload_image/ pls send the jsp servlet code when i upload the image one error found "system cannot found the specified path
Upload image
Upload image  Hai team, I beginner of Java me now i crated code for to capture image and upload to server. Here i taken a snap but when am going... help me.  Jsp Upload Image 1)page.jsp: <%@ page language="java
upload a image
upload a image  sir,how can i upload a image into a specified folder...;&lt;HEAD>&lt;TITLE>Display file upload form to the user&lt...;center">&lt;B>UPLOAD THE FILE&lt;/B>&lt;center>&lt
Retriving data stored in database
Retriving data stored in database  Hi, How to retrive data from my sql database using Hibernate,Spring and tapestry please give me an example.I am new to this Hibernate and Spring
To scan a image and upload to server
To scan a image and upload to server  I am beginner of JavaME I want a code to scan a image and upload to server
scan a image and upload to server
scan a image and upload to server  Hai i am beginner of Java ME..I want the code To scan a image and upload to server
image upload - Struts
image upload  Can any one help me how to upload an image using struts
Text Editor Image upload
Text Editor Image upload   how to browse an image from text editor instead of giving url of particular image using javascript
Capture a image and upload to server
Capture a image and upload to server  Hai team i have trouble to capture a image and upload to server in java me........... So i want a code immediately,.......To capture a image and upload to server in j2me...... If you give
upload image using JSP Hibernate
upload image using JSP Hibernate  sir, I want to take image from user and save to database(MYSQL) using Hibernate and JSP Thanks in advance
scan a image and upload to server
scan a image and upload to server  I have code for capture image and upload to server..after capture that image decoded it going to be convert digit value....My client want to scan a image and upload to server in j2me please
image in database
image in database  how to set image in database and retrieve it using servlet(java)in msaccess
Fetching image from database
name in database so, now how can i display that image using JSP or HTML page(is it possible to display using tag using concatination). image path i have stored in database is:E:\1003\54175\20110407121554 image name i have stored
hibernate image Upload
hibernate image Upload  Sir, I had posted question regarding upload image, th answer you had given is using SQL, but I am using Hibernate to insert data in the same table where I want to insert image. Plz hlp me. Thanks
Image upload file - JSP-Servlet
Image upload file  I want a code for image upload jsp or servlet.  Hi friend, For image upload jsp or servlet visit to : http://www.roseindia.net/jsp/file_upload/employee_upload_profile_image.shtml http
Upload image to the server in J2ME?
Upload image to the server in J2ME?  HI Team, I am new to the j2me and now I am working on the program that, upload captured image to the server by using j2me. I did this by using simulator but when I made hit from the mobile
Image Upload Error
Image Upload Error  hi, I created one education website in jsp....in my registration page image uploading option also i attached in real path...it's working well in local file....when i upload file in live Using FTP
check all database stored procedures - JDBC
check all database stored procedures  How to check all database stored procedures in Java
upload image in php
upload image in php  after select image,how to save it in a folder...; <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form> 2
image upload with jsp from form
image upload with jsp from form  hi i used the code specified in your...-upload-and-stored-in-database.html but i am getting following error...) org.apache.jsp.upload_jsp._jspService(upload_jsp.java:85
struts2 upload image public_html
struts2 upload image public_html  How to upload images in public_html file in Struts 2 application. Thanks in advance
problem to do coding upload image in php
problem to do coding upload image in php  can i get example coding how to upload image in php?i have try it but i dont no wheter it is correct or not because i can't save the image in folder and not connected to database mysql

Ads