2 Replies - 9904 Views - Last Post: 28 June 2009 - 01:35 PM

#1 farahshafilla   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-June 09

how to save image in mysql through JSP(MVC)

Post icon  Posted 27 June 2009 - 06:59 PM

hi all....
i am a beginner in jsp...
i try to save image in mysql....
but i seem error when i run the code...
	1.
	  import java.io.*;
   2.
	  import java.sql.*;
   3.
	  import java.util.*;
   4.
	  import java.util.regex.*;
   5.
	  import org.apache.commons.fileupload.servlet.ServletFileUpload;
   6.
	  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
   7.
	  import org.apache.commons.fileupload.*;
   8.
	   
   9.
	  import javax.servlet.*;
  10.
	  import javax.servlet.http.*;
  11.
	   
  12.
	  public class UploadImage extends HttpServlet {
  13.
	  @Override
  14.
	  public void doPost(HttpServletRequest request, HttpServletResponse response)
  15.
	  throws ServletException, IOException {
  16.
	  PrintWriter out = response.getWriter();
  17.
	  boolean isMultipart = ServletFileUpload.isMultipartContent(request);
  18.
	  System.out.println("request: " + request);
  19.
	  if (!isMultipart) {
  20.
	  System.out.println("File Not Uploaded");
  21.
	  } else {
  22.
	  FileItemFactory factory = new DiskFileItemFactory();
  23.
	  ServletFileUpload upload = new ServletFileUpload(factory);
  24.
	  List items = null;
  25.
	   
  26.
	  try {
  27.
	  items = upload.parseRequest(request);
  28.
	  System.out.println("items: " + items);
  29.
	  } catch (FileUploadException e) {
  30.
	  e.printStackTrace();
  31.
	  }
  32.
	  Iterator itr = items.iterator();
  33.
	  while (itr.hasNext()) {
  34.
	  FileItem item = (FileItem) itr.next();
  35.
	  if (item.isFormField()) {
  36.
	  String name = item.getFieldName();
  37.
	  System.out.println("name: " + name);
  38.
	  String value = item.getString();
  39.
	  System.out.println("value: " + value);
  40.
	  } else {
  41.
	  try {
  42.
	  String itemName = item.getName();
  43.
	  Random generator = new Random();
  44.
	  int r = Math.abs(generator.nextInt());
  45.
	   
  46.
	  String reg = "[.*]";
  47.
	  String replacingtext = "";
  48.
	  System.out.println("Text before replacing is:-" + itemName);
  49.
	  Pattern pattern = Pattern.compile(reg);
  50.
	  Matcher matcher = pattern.matcher(itemName);
  51.
	  StringBuffer buffer = new StringBuffer();
  52.
	   
  53.
	  while (matcher.find()) {
  54.
	  matcher.appendReplacement(buffer, replacingtext);
  55.
	  }
  56.
	  int IndexOf = itemName.indexOf(".");
  57.
	  String domainName = itemName.substring(IndexOf);
  58.
	  System.out.println("domainName: " + domainName);
  59.
	   
  60.
	  String finalimage = buffer.toString() + "_" + r + domainName;
  61.
	  System.out.println("Final Image===" + finalimage);
  62.
	   
  63.
	  File savedFile = new File("C:/apache-tomcat-6.0.16/webapps/example/" + "images\\" + finalimage);
  64.
	  item.write(savedFile);
  65.
	  out.println("<html>");
  66.
	  out.println("<body>");
  67.
	  out.println("<table><tr><td>");
  68.
	  out.println("<img src=images/" + finalimage + ">");
  69.
	  out.println("</td></tr></table>");
  70.
	   
  71.
	  Connection conn = null;
  72.
	  String url = "jdbc:mysql://localhost:3306/";
  73.
	  String dbName = "thundercatz";
  74.
	  String driver = "com.mysql.jdbc.Driver";
  75.
	  String username = "root";
  76.
	  String userPassword = "";
  77.
	  String strQuery = null;
  78.
	  String strQuery1 = null;
  79.
	  String imgLen = "";
  80.
	   
  81.
	  try {
  82.
	  System.out.println("itemName::::: " + itemName);
  83.
	  Class.forName(driver).newInstance();
  84.
	  conn = DriverManager.getConnection(url + dbName, username, userPassword);
  85.
	  Statement st = conn.createStatement();
  86.
	  strQuery = "insert into testimage values image='" + finalimage + "'";
  87.
	   
  88.
	  int rs = st.executeUpdate(strQuery);
  89.
	  System.out.println("Query Executed Successfully++++++++++++++");
  90.
	  out.println("image inserted successfully");
  91.
	  out.println("</body>");
  92.
	  out.println("</html>");
  93.
	  } catch (Exception e) {
  94.
	  System.out.println(e.getMessage());
  95.
	  } finally {
  96.
	  conn.close();
  97.
	  }
  98.
	  } catch (Exception e) {
  99.
	  e.printStackTrace();
 100.
	  }
 101.
	  }
 102.
	  }
 103.
	  }
 104.
	  }
 105.
	  }

there is no error appear...but a blank page will appear when i run this code..i am getting confused why its happen. Is there any one that know what wrong in the code?

Is This A Good Question/Topic? 0
  • +

Replies To: how to save image in mysql through JSP(MVC)

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6841
  • View blog
  • Posts: 31,535
  • Joined: 10-May 07

Re: how to save image in mysql through JSP(MVC)

Posted 27 June 2009 - 09:24 PM

** Moved to Java **
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: how to save image in mysql through JSP(MVC)

Posted 28 June 2009 - 01:35 PM

import java.io.*;
import java.sql.*;
import java.util.*;
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 {
	@Override
	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);
						out.println("<html>");
						out.println("<body>");
						out.println("<table><tr><td>");
						out.println("<img src=images/" + finalimage + ">");
						out.println("</td></tr></table>");

						Connection conn = null;
						String url = "jdbc:mysql://localhost:3306/";
						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();
							conn = DriverManager.getConnection(url + dbName,
									username, userPassword);
							Statement st = conn.createStatement();
							strQuery = "insert into testimage values image='"
									+ finalimage + "'";

							int rs = st.executeUpdate(strQuery);
							System.out
									.println("Query Executed Successfully++++++++++++++");
							out.println("image inserted successfully");
							out.println("</body>");
							out.println("</html>");
						} catch (Exception e) {
							System.out.println(e.getMessage());
						} finally {
							conn.close();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}


So you have a number of debugging lines that should print some data as the program progresses -- what is the console output when this runs?

I have just glanced at the code as I reformatted it -- so I will look deeper now that it is in a format I can read.
Was This Post Helpful? 1

Page 1 of 1