my dog learned polymorphism*
The moose likes Servlets and the fly likes uploading and storing image in JSP/Mysql Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "uploading and storing image in JSP/Mysql" Watch "uploading and storing image in JSP/Mysql" New topic
Author

uploading and storing image in JSP/Mysql

Nour-eddine Ghtami
Greenhorn

Joined: Jun 02, 2004
Posts: 2
Hello,
i tried to upload a picture and pass it from (upload_pic.jsp) to (show_pic.jsp)pages in order to store it in a Mysql table as a BLOB type, but i run into an Exception of 1 error in(show_pic.jsp)page as the following :

incompatible types
found : java.lang.String
required: java.sql.Blob
Blob img=request.getParameter("pic_");

this is part of (upload_pic.jsp)
<form method="post" action='<%= response.encodeURL("show_pic.jsp") %>'>
<input type="file" name="pic_" /><br>
<input type=submit value=upload ></form>

and this is part of (show_pic.jsp)
<body>
<% Class.forName("org.gjt.mm.mysql.Driver");
Connection cnct = DriverManager.getConnection("jdbc:mysql:///propertysearch");
Statement stmt=cnct.createStatement();
Blob img=request.getParameter("pic_");

String upld="insert into binar_d values(null,'"+img+"','');";
stmt.executeQuery(upld);
String display="select * from binar_d ";
ResultSet RS =stmt.executeQuery(display);
while(RS.next())
{ Blob pic=RS.getBlob("pic_ures");
out.write("<div align=center>"+ pic +"</div>");
}
%>
</body>

anyone to show me where the problem resides please?

thank you.
Ankur Srivastava
Ranch Hand

Joined: May 11, 2004
Posts: 62
Use the request's setAttribute and getAttribute methods for objects.
The getParameter methods of request is used for retrieving String objects.

Refer ServletRequest API.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

get and set attribute have nothing to do with this issue.

First, in order to upload a file, you need to specify that the form is submitting a multi-part request. The default enctype of the form tag will not do.

Secondly, once you do that, the HttpServletRequest will no longer automatically parse your request. You'll either have to do it yourself, or use one of the 3rd party libraries floating around to do it for you.

Search this, the JSP and perhaps the Tomcat forum for more information on file uploading.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Johnson Abraham
Greenhorn

Joined: May 31, 2004
Posts: 19
As pointed out you should use the enctype of your form to be enctype="MULTIPART/FORM-DATA" so that the content of your file gets embeded into your request object.
But then now you cannot use the request objects method to retrieve the values of any parameter of the submitted form. you have to use third party packages.
You can get all that you want to achieve this on the following link.

web page
[ June 04, 2004: Message edited by: Johnson Abraham ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: uploading and storing image in JSP/Mysql
 
Similar Threads
Display error message in div tag
Display error when record not found
passing jsp variable to java script function on same page
jsp & HTML
jsp/Mysql uploading an image?