PDA

View Full Version : how to upload and save image in mysql using jsp?


farahshafilla
07-06-2009, 05:58 AM
hi...i have this code ;
uploadImage.jsp
<html>

<head><title>Image Upload</title></head>

<body>
<form action="UploadImage" 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>&nbsp;</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">&nbsp;</td>
</tr>

</table>
</form>
</body>

</html>

UploadImage.java
import java.io.*;
import java.sql.*;
import java.util.*;
import java.text.*;
import java.util.regex.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class UploadImage extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
System.out.println("request: " + request);
if (!isMultipart) {
System.out.println("File Not Uploaded");
} else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;

try {
items = upload.parseRequest(request);
System.out.println("items: " + items);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
String name = item.getFieldName();
System.out.println("name: " + name);
String value = item.getString();
System.out.println("value: " + value);
} else {
try {
String itemName = item.getName();
Random generator = new Random();
int r = Math.abs(generator.nextInt());

String reg = "[.*]";
String replacingtext = "";
System.out.println("Text before replacing is:-" + itemName);
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(itemName);
StringBuffer buffer = new StringBuffer();

while (matcher.find()) {
matcher.appendReplacement(buffer, replacingtext);
}
int IndexOf = itemName.indexOf(".");
String domainName = itemName.substring(IndexOf);
System.out.println("domainName: " + domainName);

String finalimage = buffer.toString() + "_" + r + domainName;
System.out.println("Final Image===" + finalimage);

File savedFile = new File("C:/apache-tomcat-6.0.16/webapps/example/" + "images\\" + finalimage);
item.write(savedFile);
System.out.println("<html>");
System.out.println("<body>");
System.out.println("<table><tr><td>");
System.out.println("<img src=images/" + finalimage + ">");
System.out.println("</td></tr></table>");

String userName = "root";
String password = "";
String url = "jdbc:mysql://localhost/thundercatz";
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
// connection = DriverManager.getConnection(url, userName, password);

Connection connection = null;
Statement statement;
// String url = "jdbc:mysql://localhost/";
// String dbName = "thundercatz";
// String driver = "com.mysql.jdbc.Driver";
// String username = "root";
// String userPassword = "";
String strQuery = null;
// String strQuery1 = null;
//String imgLen="";

try {
System.out.println("itemName::::: " + itemName);
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(url, userName, password);
statement = connection.createStatement();
strQuery = "INSERT INTO testimage VALUES ('" + finalimage + "')";
int rs = statement.executeUpdate(strQuery);
System.out.println("Query Executed Successfully++++++++++++++");
System.out.println("image inserted successfully");
System.out.println("</body>");
System.out.println("</html>");
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
the blank page will appear when i run this code.
anyone plz help me

ckeyrouz
07-06-2009, 03:25 PM
In fact it seems data is being saved to the database.
You are submitting the form so the data in the screen is being sent to the server but no return is being done.

At the end of this function you should put a redirection call:
in the finally after the connection.close() try this:

response.sendRedirect("your url");

leneena
07-06-2009, 07:50 PM
Hi Faraf,
i guess u need to use RequestDispatcher to communicate from servlet to JSP.
i guess this will work for u, just try it out and let me know....
Regards,
Leneena

farahshafilla
07-07-2009, 08:07 AM
Hi Faraf,
i guess u need to use RequestDispatcher to communicate from servlet to JSP.
i guess this will work for u, just try it out and let me know....
Regards,
Leneena

i already do it.
but it still have a problem..
the image still not save in the database