i need help..to anyone who expert in servlet & Jsp..
my problem is..i do not know how to upload image via html/jsp..
>before that i already ask mr.google but.. i got sux answer.
so, this code work's and upload succesfully but it must manually put the destination file type string in this class >> File file = new File("C:/images/5.jpg");
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class InsertImage extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
PrintWriter pw = response.getWriter();
String connectionURL = "jdbc:mysql://localhost:3306/test";
Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(connectionURL, "root", "");
PreparedStatement ps = con.prepareStatement("INSERT INTO pictures VALUES(?,?)");
File file = new File("C:/images/5.jpg");
FileInputStream fs = new FileInputStream(file);
ps.setInt(1,1);
ps.setBinaryStream(2,fs,fs.available());
int i = ps.executeUpdate();
if(i!=0){
pw.println("image inserted successfully");
}else{
pw.println("problem in image insertion");
}
} catch (Exception e){
System.out.println(e);
}
}
}
then, when i try insert image using html/jsp form, it failed to insert that image into database.. and i got blank white screen.
here the html form..
<html>
<head><title>Image Upload</title></head>
<body>
<form action="UploadImage.do" method="post" enctype="multipart/form-data" name="productForm" id="productForm"><br><br>
<table width="400px" align="center" border=0 style="background-color:ffeeff;">
<tr>
<td align="center" colspan=2 style="font-weight:bold;font-size:20pt;">Image Details</td>
</tr>
<tr>
<td align="center" colspan=2> </td>
</tr>
<tr>
<td>Image Link: </td>
<td>
<input type="file" name="file" id="file">
<td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
</form>
</body>
</html>
and this is my xml.web
<servlet>
<servlet-name>InsertImage</servlet-name>
<servlet-class>InsertImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InsertImage</servlet-name>
<url-pattern>/UploadImage.do</url-pattern>
</servlet-mapping>
and this is mysql database.. field
CREATE TABLE `picture` (
`id` int(10) NOT NULL auto_increment,
`image` blob,
PRIMARY KEY (`id`)
)
hope this info can help..thanks