Problem in uploading image to to mysql database
Hi, need some help here, i have a program where a user can input name, city and upload image. when a user click the submit button the name, city and the image(Save as BLOB) must be save in the database. and the image will also save in the desired folder. i have no problem in saving the image in the folder, my problem is it can't save the record to mysql database.
heres my code...
index.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"> </td>
</tr>
<tr>
<td><b>Name:</b></td>
<td><INPUT NAME="name" TYPE="text"></td>
</tr>
<tr>
<td><b>City:</b></td>
<td><INPUT NAME="city" TYPE="text"></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"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Send File"> </td>
</tr>
<table>
</center>
</FORM>
</BODY>
</HTML>
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("C:/f/" + 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>
<%
String Name = request.getParameter("name");
String City = request.getParameter("city");
if (Name != null && City != null) {
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/dropdown";
ResultSet rs = null;
PreparedStatement psmnt = null;
FileInputStream fis;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "password");
File f = new File("C:/f/" + saveFile);
psmnt = connection.prepareStatement("insert into save_image(name, city, image) values(?, ?, ?)");
psmnt.setString(1, Name);
psmnt.setString(2, City);
fis = new FileInputStream(f);
psmnt.setBinaryStream(3, (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();
}
}
}
%>
View Answers
Related Tutorials/Questions & Answers:
Problem in uploading image to to mysql databaseProblem in
uploading image to to
mysql database Hi, need some help... be save in the
database. and the
image will also save in the desired folder. i have no
problem in saving the
image in the folder, my
problem is it can't save
Problem in uploading image to to mysql databaseProblem in
uploading image to to
mysql database Hi, need some help... be save in the
database. and the
image will also save in the desired folder. i have no
problem in saving the
image in the folder, my
problem is it can't save
Advertisements
uploading problem about file into
database lib.
i use navicat
Mysql ...
i use this code...
<...
uploading problem i use glassfish server..
using netbeans for jsp... curresponding
image files start new
upload");
%><
Image uploadingImage uploading Hi,can anyone explain the following code.
The code related to
uploading an
image file to oracle
database using java servlet.
CODE
import java.io.*;
import java.io.IOException;
import
How to retrieve image from mysql database in JSP?How to retrieve
image from
mysql database in JSP? Hi,
I need JSP same codes for learning to get
image which is stored in
MySQL Database.
How to retrieve
image from
mysql database in JSP?
Thanks
Hi,
You can write
Image upload in mysql database using jsp servletImage 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
Uploading an image into the table - JSP-ServletUploading an
image into the table how to upload an
image into the table in java Hi friend,
Code to help in solving the
problem... to the specified
database. */
psmnt = connection.prepareStatement
("insert into save_
image uploading image in the formuploading image in the form Hi All,
I am working to build a form like railway registration form which accepts user id and password and
image for authentication.
And i want all users should be able to see the
image once
Image uploading in FTP ServerImage uploading in FTP Server I want to upload images to a ftp server every 1 hour automatically using java..please help
Problem in uploading java applicationProblem in
uploading java application I have uploaded my java application (folder created under webapps) using Filezilla FtpClient.Application... this
problem Uploading Multiple Image On Server?Uploading Multiple
Image On Server? Hello sir,
I am stuck with a
problem of
uploading multiple images on server.
i have done a code which works fine for
uploading single
image,but it doesn't work with
uploading multiple
Uploading a Software and storing in the databaseUploading a Software and storing in the database I want to upload a software(may be of maximum 20mb) through JSP, and store it in the
database.
The coding present in the site for
uploading and storing in the
database File Uploading ProblemFile
Uploading Problem I have a file
uploading code but it create
problem
$(document).ready(function(){
$('#upload').click(function(){
var... it gives the
problem
org.apache.commons.fileupload.FileUploadException
File Uploading ProblemFile
Uploading Problem I have a file
uploading code but it create
problem
$(document).ready(function(){
$('#upload').click(function(){
var... it gives the
problem
org.apache.commons.fileupload.FileUploadException
change the name of the image after uploading the imagechange the name of the
image after
uploading the image my form consists of a textbox and
uploading the image.after
uploading the
image i want to change the name of the
image with the content of the textbox before saving
image is display from path of mysql databaseimage is display from path of
mysql database <%@ page import="java.io.,java.sql.,java.util.zip.*" %>
<%
String saveFile="";
String...;%
Connection connection = null;
String connectionURL = "jdbc:
mysql://localhost
Uploading image using jspUploading image using jsp how to upload
image using jsp. Already i tried, But that
image file does not read.
It returns only -1 without reading...("GET
IMAGE PROBLEM :: "+ex);
ex.printStackTrace();
}
return result;
}
%>
<
struts image uploadingstruts
image uploading please let me know how to upload
image in
database using struts ..i have written form class and 'm hanging out in action...");
Connection con=DriverManager.getConnection("jdbc:
mysql struts image uploadingstruts
image uploading please let me know how to upload
image in
database using struts ..i have written form class and 'm hanging out in action...");
Connection con=DriverManager.getConnection("jdbc:
mysql struts image uploadingstruts
image uploading please let me know how to upload
image in
database using struts ..i have written form class and 'm hanging out in action...");
Connection con=DriverManager.getConnection("jdbc:
mysql struts image uploadingstruts
image uploading please let me know how to upload
image in
database using struts ..i have written form class and 'm hanging out in action...");
Connection con=DriverManager.getConnection("jdbc:
mysql://localhost
Uploading an image - JSP-ServletUploading an image I am doing a jsp project. In this
uploading an
image is a part. For that i had done a coding for
uploading... and
uploading is done by different jsp coding. I don't know how to code for file size
uploading a text file into a databaseuploading a text file into a database how to upload a text file into a
database using jchooser
import java.io.*;
import java.sql....");
Connection conn = DriverManager.getConnection("jdbc:
mysql://localhost
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...
Note : In the jsp code given below,
image will
be retrieved from
database image in databaseimage in database how to set
image in
database and retrieve it using servlet(java)in msaccess
image uploading perminssion in server - JSP-Servletimage uploading perminssion in server thanks dear,
but i am working in Linux Ubuntu, how can i set the path in server.
my
problem is i am not able to access the folders wherever we are
uploading the files, we are not able
mysql problem - JDBCmysql problem
hai friends
i have some
problem with
image storing in
mysql. i.e while i am using
image(blob) for insert the
image it says... of creation of table in
mysql. it will take any
image and store in
database image uploading perminssion in server - JSP-Servletimage uploading perminssion in server Dear All,
I am facing some
problem image uploading in server that i working fine in my system, OS Ubuntu...; Hi friend,
Plz give full source code with details to solve the
problem Inserting Image in Database Table
This program helps the user to
insert
an
image in the
MySQL database table. To insert an
image, you need to
establish a connection with
MySQL database... Inserting
Image in
Database Table
Image in mysqlImage in mysql Hi. How to insert and retrieve images in
mysql db using JSP or JAVA Servlet? Thanks in advance
Having problem with image upload....Having
problem with
image upload.... I am
uploading profile pictures in my site.But after
uploading when I visit my home page where it is supposed... is correct.I have renamed the
image after
uploading with time stamp so as to prevent
problem in database problem in
database thanks for web site.
I want change this code to insert data into PostgreSql
database using jsp,servlets.
but i getting...){
out.println("Couldn't load
database driver: " + e.getMessage());
}
catch
database problemdatabase problem I did connect my jsp page to
database and it showed the result on another jsp page but i want to show the data of that field on which i click to mouse from the jsp page where all the data field has been showed