posted 12 years ago
- Number of slices to send: Optional 'thank-you' note:
Hello I need to send data from a form to a database but... this form have a field input also. I trying to send all the data together (image and input text values) but I can't. Is to say in the database I just get the image but not the input text values! I read in someplace that is not possible but...I had seen that working fine in some websites! What is wrong? Here the code that I am -testing: 1)the jsp form:
- and the uploadimage servlet
NOTE: the 2 values showed with System.out.print are null.
Please give me an idea.
THANKS
Skip to Main Content
This is my Jsp code for image upload in database:
---Upload.jsp---- <html>
<head>
</head> <body bgproperties="fixed" bgcolor="
CCFFFF">
<form method="POST" action="UploadPicture.jsp" enctype="multiform/form-data">
<%! int update=0; %>
<%@ page import="java.util.*" %> <%@ page import="java.sql.*" %> <%@ page import="java.text.*" %> <%@ page import="java.sql.Date" %> <%@ page import="java.io.*"%>
<%@ page language = "java" %>
<% try { String ct="3"; String path;
File image=new File(request.getParameter("upload")); path=request.getParameter("upload"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:itPlusElectronics","","");
PreparedStatement pstmt=con.prepareStatement("insert into graphics values(?,?,?)"); pstmt.setString(2,path); pstmt.setString(3,ct);
InputStream is=new FileInputStream(path); pstmt.setBinaryStream(1, is, (int)(image.length())); int s=pstmt.executeUpdate(); if(s>0) { out.println("Uploaded"); } else { %> unsucessfull <%} is.close(); pstmt.close(); } catch(Exception e) { }%> </p>
<p><br> <img src="UploadedPicture.jsp">image</img>
<p></p> </form> </body> </html>
--------- My database name is itPlusElectronics and the table name is "graphics". I have seen as a result of the above code that the image is stored in database as "Long binary data". and database table is look like as follows---
picture path id Long binary data D:\AMRIT\1-1-Picture.jpg 3
---------- To retrive and display i use this JSP code as--
----UploadedPicture.jsp------
<html>
<head>
</head> <body bgproperties="fixed" bgcolor="
CCFFFF">
<%! int update=0; %>
<%@ page import="java.util.*" %> <%@ page import="java.sql.*" %> <%@ page import="java.text.*" %>
<%@ page import="java.io.*"%> <%@ page language = "java" %> <%@page import="javax.servlet.ServletOutputStream"%>
<%
try {
String path; path=request.getParameter("upload1"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:itPlusElectronics","","");
PreparedStatement pst = con.prepareStatement("SELECT * FROM graphics WHERE id ='3'"); // pst.setString(3, id); ResultSet rs = pst.executeQuery(); path=rs.getString("path"); if(rs.next()) {
byte[] bytearray = new byte[4096]; int size=0; InputStream sImage; sImage = rs.getBinaryStream(1); response.reset(); response.setContentType("image/jpeg"); response.addHeader("Content-Disposition","filename=path"); while((size=sImage.read(bytearray))!= -1 ) { response.getOutputStream().write(bytearray,0,size) ; } response.flushBuffer(); sImage.close(); rs.close(); }
} catch(Exception e) { } %>
</body> </html>
---------- Now after browsing a jpg image file from client side and pressing submit button ; I am unable display the image in the Upload.jsp file.Though I use <img src="UploadedPicture.jsp">image</img> HTML code in Upload.jsp file .
Now I am unable to find out the mistakes which is needed for displaying the picture in the Upload.jsp page.. If any one can help with the proper jsp code to retrive and display the image ,please please help me !!!!!!!!!!!!!!!!!!!!!!!!!!