Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

uploading and storing image in JSP/Mysql

 
Nour-eddine Ghtami
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Johnson Abraham
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic