473,385 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to upload image/video file in to database

Hi every one,

iam developing an application where i want to upload image and video files in to a oracle data base. I have used BLOB data type to store the image and video files.

iam using jsp as front end and java servlet for the application.

iam converting the image file into byte[] and then inserting in to data base.

problem for me is iam able to do this on a local system. ie iam running the java in my local PC and able to upload the image files from that same PC.

if i run tha application from one PC and try to upload from different PC then iam not able to upload.
Apr 17 '07 #1
4 18762
Hi every one,

iam developing an application where i want to upload image and video files in to a oracle data base. I have used BLOB data type to store the image and video files.

iam using jsp as front end and java servlet for the application.

iam converting the image file into byte[] and then inserting in to data base.

problem for me is iam able to do this on a local system. ie iam running the java in my local PC and able to upload the image files from that same PC.

if i run tha application from one PC and try to upload from different PC then iam not able to upload.

pls suggest me what can be done
Apr 17 '07 #2
r035198x
13,262 8TB
Please do not double post.

Threads merged.
Apr 17 '07 #3
Image upload from JSP to Data Base step by step :

step 1 :
In the JSP
<form name="regform2" method="post" enctype="multipart/form-data">
<input type="file" name="ImageFile" id="ImageFile" onChange="uploadImage()"/>
</form>

java script :
function uploadImage(){
document.regform.action ="<%=request.getContextPath()%>/dfdmin?cmd=uploadimage";
document.regform.submit();
}


Step 2.
In the servlet - add the below code.
String rtempfile = File.createTempFile("temp","1").getParent();
MultipartRequest multi = new MultipartRequest(request, rtempfile, 500000 * 1024);
File rnewfile=null;
rnewfile = new File(CommonArt.IMAGE_PATH+"jsp"+File.separator+"im ages"+File.separator+"uploadImage"+File.separator) ;
if(rnewfile.exists()){
}else{
rnewfile.mkdirs();
}

File f = multi.getFile("ImageFile");
System.out.println(f.getName());
FileInputStream fin =new FileInputStream(f);
RandomAccessFile r = new RandomAccessFile(rnewfile+File.separator+f.getName (),"rw");
filename = f.getName();
// FileOutputStream fos =new FileOutputStream(rnewfile);
byte sizefile[] = new byte[5000000];
fin.read(sizefile);
// fos.write(sizefile);
r.write(sizefile);
//fos.close();
r.close();
fin.close();


Step 3.
Insert into Database

InputStream is = new FileInputStream(f);
String sql = " INSERT INTO image_upload (IMAGE) VALUES (?) ";
pStmt = conn.prepareStatement(sql);
pStmt.setBinaryStream(1, is, (int)(f.length()));
pStmt.execute();
conn.commit();


This will upload multipart file to your data base.
Note : get cos.jar from oreilly website

Hope this will help you....!!!

Regards,
Suvranil.
Aug 16 '10 #4
Connection conn = getConnection();
Statement stmt = conn.createStatement();
File file = new File("Your Video File Location and file name");
FileInputStream fileInputStream = new FileInputStream(file);
ByteArrayOutputStream out = new ByteArrayOutputStream();
while (fileInputStream.available()>0) {
out.write(fileInputStream.read());
}
byte[] jpgByteArray = out.toByteArray();
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
String INSERT_PICTURE = "insert into MyPictures(name, video) values (?, ?)";
PreparedStatement ps = null;
try {
conn.setAutoCommit(false);
ps = conn.prepareStatement(INSERT_PICTURE);
ps.setString(1, "Test1");
ps.setBytes(2,jpgByteArray);
ps.executeUpdate();
ResultSet rs = stmt.executeQuery("SELECT video FROM MyPictures");
while (rs.next()) {
File file1 = new File("Mention the destination File Location and FileName");
FileOutputStream foStream = new FileOutputStream(file1);
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
if(rs.getString("photo") != null){
InputStream is2 = rs.getBinaryStream(1);
byte[] bytes=new byte[is2.available()];
is2.read(bytes);
oStream.write(bytes);
oStream.writeTo(foStream);
}
conn.commit();
}finally {
ps.close();
}

NOTE :
If you are using MySQL DB then include code in the my.ini file as follows,This is for increase the storeage file size

wait_timeout=28800
interactive_timeout = 28800
max_allowed_packet=64M
socket = /tmp/mysql.sock
skip-locking
Nov 11 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Stephane | last post by:
Hello, I need to allow an end user to upload video files on a server and in the same time to put the file name and a few infos in a database. It must be very simple for the end user, and...
3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
35
by: Stan Sainte-Rose | last post by:
Hi, What is the better way to save image into a database ? Just save the path into a field or save the image itself ? I have 20 000 images (~ 10/12 Ko per image ) to save. Stan
16
by: lawrence k | last post by:
I've a file upload script on my site. I just now used it to upload a small text document (10k). Everything worked fine. Then I tried to upload a 5.3 meg Quicktime video. Didn't work. I've...
0
by: SEMIH DEMIR | last post by:
Sitelerden birinde verilen yabancı kaynakli bir scriptti duzenledim yanlız birseyin içinden bir turlu cıkamadım işin aslı ilk defa persistin upload componentini kullanacam yanlız suanki haliyle...
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
5
by: mikefuss | last post by:
I'm having trouble getting my PHP code to upload videos. I have a page that uploads Images, which uploads to a table called Photos and everything works fine. I tried modifying that code to upload...
1
by: Rozzy | last post by:
I have this problem were I've created a script to enable users to upload files, but since my host has disabled the natural php functions for this I have been forced to turn to ftp. The problem I...
8
johngault
by: johngault | last post by:
I've been working with this PHP page for several days now and I'm stumped. The page is supposed to allow the user to upload up to six images for their profile. When the user adds an image it (the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.