• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

insert images to database from unix server to database table

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
skolakanuru
Asked:
skolakanuru
  • 9
  • 7
6 Solutions
 
skolakanuruAuthor Commented:
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
 
KuldeepchaturvediCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
skolakanuruAuthor Commented:
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
 
KuldeepchaturvediCommented:
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
 
skolakanuruAuthor Commented:
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
 
skolakanuruAuthor Commented:
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
 
KuldeepchaturvediCommented:
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
 
skolakanuruAuthor Commented:
it is not displaying the image.
just the blank box of image is shown. wat can be the reason?
0
 
KuldeepchaturvediCommented:
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
 
skolakanuruAuthor Commented:
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
 
KuldeepchaturvediCommented:
thats strange... given that it should display on jsp..
0
 
skolakanuruAuthor Commented:
what should be there in JSP and the web.xml defination so that I get the image?
 
0
 
KuldeepchaturvediCommented:
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
 
skolakanuruAuthor Commented:
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
 
KuldeepchaturvediCommented:
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
 
skolakanuruAuthor Commented:
I tried changin the URL mapping i used earlier and this is working fine.
 
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now