Solved

insert images to database from unix server to database table

Posted on 2009-04-07
18
418 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can i Import Access Table Into Oracle Using Toad 36 179
servlet filter example 37 63
VMWare environment audit 8 31
error in my cursor 5 34
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

831 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