how to upload image from jsp to mssql
hi there!!,
i'm using jsp and servlet to upload images to the database. however i have difficulty in uploading database. hope u can help
in my database:
i have imagetbl that contain image column(to store the image) as varbinary and fileimage(to store the directory of the image) as while as imageID(primary key)
inside netbeans:
image.jsp,
uplaodimage.java(servlet)
View Answers
July 23, 2012 at 11:46 AM
Here is a code that uploads any file through a servlet and save it to mysql database.
1)page.jsp:
<%@ page language="java" %>
<HTML>
<FORM ENCTYPE="multipart/form-data" ACTION="../UploadServlet" METHOD=POST>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center><td colspan="2"><B>UPLOAD THE FILE</B><center></td>
</tr>
<tr><td colspan="2" > </td></tr>
<tr><td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr><td colspan="2" > </td></tr>
<tr><td colspan="2"><input type="submit" value="Send File"> </td></tr>
<table>
</center>
</FORM>
</HTML>
2)UploadServlet.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class UploadServlet extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
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();
out.println("You have successfully upload the file:"+saveFile);
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(?)");
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();
}
}
}
}
Ads
Related Tutorials/Questions & Answers:
how to upload image from jsp to mssql
how to
upload image from jsp to mssql hi there!!,
i'm using
jsp and servlet to
upload images to the database. however i have difficulty in uploading database. hope u can help
in my database:
i have imagetbl that contain
image
how to upload image from jsp to mssql
how to
upload image from jsp to mssql hi there!!,
i'm using
jsp and servlet to
upload images to the database. however i have difficulty in uploading database. hope u can help
in my database:
i have imagetbl that contain
image
Advertisements
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
image upload with jsp from form
image upload with
jsp from form hi i used the code specified in your following post
http://www.roseindia.net/answers/viewqa/
JSP-Servlet/9749-image...)
org.apache.jsp.upload_
jsp._jspService(
upload_jsp.java:85
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
How to retrieve blob image from database in JSP?
How to retrieve blob
image from database in
JSP? Hello,
JSP is used... the blog
image from database.
How to retrieve blob
image from database in
JSP?
Hi,
Please check the tutorial Retrieve
image from database using
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
How to retrieve single row from MSSQL
How to retrieve single row
from MSSQL Hello sir,
Sir i had created the table in
MSSQL,the fields with patient name,pid,Dname(doctor name... created table in
MSSQL,GUI in eclipse.How to achieve it..
Thanks & Regards
How to retrieve single row from MSSQL
How to retrieve single row
from MSSQL Hello sir,
Sir i had created the table in
MSSQL,the fields with patient name,pid,Dname(doctor name... created table in
MSSQL,GUI in eclipse.How to achieve it..
Thanks & Regards
How to retrieve single row from MSSQL
How to retrieve single row
from MSSQL Hello sir,
Sir i had created the table in
MSSQL,the fields with patient name,pid,Dname(doctor name... created table in
MSSQL,GUI in eclipse.How to achieve it..
Thanks & Regards
Multiple image upload using JSP
Multiple
image upload using JSP I have written a code to
upload the multiple images using
jsp, but when I execute this code, it throws Corrupt form...; fileSavePath_
upload_user_dir = fileSavePath_
upload_dir +
> File.separator + user_
image
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 upload file using JSP?
How to
upload file using
JSP? Hi all,
I m the beginner in
JSP, I want to
upload file on server in specific folder.
1)page.jsp... file
upload form to the user</TITLE></HEAD>
<