读取过程 硬盘文件项工厂 Servlet文件上传 文件项迭代器 文件项流 输入流 连接数据库 加载驱动类 获取connection对象 拼写预处理语句,生成预处理 对象preparedStatement 执行
  package Servlet;
  import java.io.IOException;
  import java.io.InputStream;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.commons.fileupload.FileItemIterator;
  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  import org.apache.commons.fileupload.servlet.ServletFileUpload;
  /**
  * Servlet implementation class ImageServlet
  */
  public class ImageServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  System.out.println(123);
  try {
  DiskFileItemFactory factory = new DiskFileItemFactory();
  ServletFileUpload upload = new ServletFileUpload(factory);
  FileItemIterator iter = upload.getItemIterator(request);
  InputStream inputStream = iter.next().openStream();
  Class.forName("com.mysql.jdbc.Driver");
  Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Demos","root","1a");
  String sql = "insert into image_Demo(image) values (?)";
  PreparedStatement ps = conn.prepareStatement(sql);
  ps.setBinaryStream(1, inputStream, inputStream.available());
  ps.execute();              
  } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  }
  }