Solved

insert images to database from unix server to database table

Posted on 2009-04-07
18
426 Views
Last Modified: 2013-12-24
I want to insert images into database table using JSP, JDBC
my images are on unix server, so how can I work on them.


thanks
0
Comment
Question by:skolakanuru
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
18 Comments
 
LVL 12

Assisted Solution

by:basav_com
basav_com earned 25 total points
ID: 24090628
0
 

Author Comment

by:skolakanuru
ID: 24092017
thank you,
but my files are on unix server so how would I connect to unix from JSP
and I wanted to use JSP to insert those image files and view them again through JSP.
 
 
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 24094196
is it on the same server where your jsps will run? if yes then you can just read them off the disk.

if they are not on the same server. then you may have map your image drive to the server where your jsps will run.


0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:skolakanuru
ID: 24109376
I inserted the image into database, but when I tried to display in JSP got the following error.
Original Exception: Error Message: SRVE0199E: OutputStream already obtained
Error Code: 500
 
 what is missing for thee output stream?
 
thanks

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %> 
<IMG src="http://localhost/jsp/.jsp">
<% // declare a connection by using Connection interface Connection 
// declaration part is here..for the database
	  ResultSet rs = null;
	  PreparedStatement psmnt = null;
	  InputStream sImage;
	  Blob img = null;
	  byte[] imgData = null ;
	  // declare InputStream object to store binary stream of given image.
 
	  //
try {
Class.forName(driverName);
          Connection myConn = DriverManager.getConnection (connectionURL);
          psmnt = myConn.prepareStatement("SELECT tax_scan_doc FROM tax_exemption_doc WHERE tax_exempt_doc_id=5639796");
//psmnt.setString(1, request.getParameter("image1")); 
         rs = psmnt.executeQuery();
        if(rs.next()) {
              		  img = rs.getBlob(1);
      				 imgData = img.getBytes(1,(int)img.length());
                   //   byte[] bytearray = new byte[1048576];
                   response.setContentType("image/jpeg");
                   OutputStream o = response.getOutputStream();
       					o.write(imgData);
                   
                      // FileInputStream fis = new FileInputStream(f);
                      int size=0;
                      sImage = rs.getBinaryStream(1);
                      response.reset();
                      response.getOutputStream().write(imgData,0,size);
                     
                                                                 }
                      
   }
catch(Exception ex){
out.println("error :"+ex);
}
finally {
// close all the connections.
rs.close();
psmnt.close();
}
%>

Open in new window

0
 
LVL 19

Assisted Solution

by:Kuldeepchaturvedi
Kuldeepchaturvedi earned 250 total points
ID: 24109477
take out this line

<IMG src="http://localhost/jsp/.jsp">

Also in jsp make sure you are not leaving any white spaces between tags otherwise your image wont come right..

its best to create a servlet for showing images...
0
 

Author Comment

by:skolakanuru
ID: 24109514
I removed that line
how do I check the space in between tags?
I am getting some garbage value instead of the image. I wanted to do in JSP to show the image.
 
thanks
0
 

Author Comment

by:skolakanuru
ID: 24120720
i used servlet now to get the image, but how can i get in JSP if i run the servlet it is asking  me to save the image
how can i put it in JSP?
0
 
LVL 19

Assisted Solution

by:Kuldeepchaturvedi
Kuldeepchaturvedi earned 250 total points
ID: 24120976
in jsp anything that is outside of <%%> tags, gets printed to output.. hence it messes up the bitstream of your image..

from servlet if you are getting the image.. then use that servlet url in your HTML
like
<IMG src="http://localhost/servlet/yourservlet">

0
 

Author Comment

by:skolakanuru
ID: 24120989
it is not displaying the image.
just the blank box of image is shown. wat can be the reason?
0
 
LVL 19

Assisted Solution

by:Kuldeepchaturvedi
Kuldeepchaturvedi earned 250 total points
ID: 24121015
when you said that its showing the option to save it.. if you save and then try to open the image does it show it?
I am trying to see if your servlet is getting the image correctly from DB
0
 

Author Comment

by:skolakanuru
ID: 24121071
yes servlet is getting the image, i can view it after saving but when i give the servlet url in JSP there is no image.
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 24121596
thats strange... given that it should display on jsp..
0
 

Author Comment

by:skolakanuru
ID: 24122615
what should be there in JSP and the web.xml defination so that I get the image?
 
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 24122663
for jsp you do not need anything in the web.xml.. it is needed for the servlet though...

from your previous comments I believe your servlet is already working..

i.e. if you type the URL for your servlet,something like( http://localhost/yourapp/servlet/yourservlet) it shows you a prompt to save the image.. when you save it and open, you can see the image..

if the above statement is true, then the same image should be able to display inside an <IMG> tag of any HTML or JSP
0
 

Author Comment

by:skolakanuru
ID: 24123010
yes it prompts to save tat servlet url.
wat wud be the img src in JSP?
<IMG src =.....?? not sure if im using the write mapping in web.xml and using the rite way to call it.
in tat img src i see a blank space instead of the image.
0
 
LVL 19

Assisted Solution

by:Kuldeepchaturvedi
Kuldeepchaturvedi earned 250 total points
ID: 24124192
the url should be the same url that you tried on the browser..... i.e. the url that you are using to call your servlet..

in next post please paste the url that prompts you to save the the image and paste the jsp code where you want to display the image.
0
 

Accepted Solution

by:
skolakanuru earned 0 total points
ID: 24124633
I tried changin the URL mapping i used earlier and this is working fine.
 
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question