Solved

insert images to database from unix server to database table

Posted on 2009-04-07
18
420 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

828 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